Models
A model describes one table of the database: its columns, its keys, its
relations to other tables, its indexes and its constraints. It is what a
Django model class is: class Train(models.Model), its fields and its
Meta. In rind, a model is a plain Rust struct with #[derive(Model)].
Everything else is built on the models. Queries are written with their columns, migrations are computed from them, and resources serve them over HTTP.
This chapter is long, so it is cut into sections. Each page takes one part of a model, lists every option, and shows the SQL each one produces.
| Page | What it covers | In Django |
|---|---|---|
| A first model | one model line by line, its table, what the derive gives, from the struct to the table | class Train(models.Model) |
| Fields and column types | which Rust type gives which SQL type, NULL, every type rind knows | the field classes: CharField, IntegerField, JSONField… |
| Column options | lengths, defaults, unique columns, indexed columns, generated columns | max_length, default, unique, db_index |
| Types of your own | storing an enum or any type of yours in a column | custom fields, choices |
| Primary keys | id, another key, keys the application gives | primary_key=True, AutoField |
| Foreign keys | pointing to another model, what a deletion does | ForeignKey, on_delete |
| Reverse and many-to-many relations | walking a key backwards, link tables | related_name, ManyToManyField |
| Trees | rows that have a parent among themselves | django-mptt, django-treebeard |
| Indexes and constraints | indexes on several columns, unique together, checks | Meta.indexes, Meta.constraints |
| Scopes and scoped models | named queries on a model; models with no default query | managers, objects.published() |
| Sharing fields | fields written once, used by several models | abstract models, mixins |
| Writing objects | insert, save, update, delete on an object | obj.save(), obj.delete() |
| Behaviours and hooks | what every write of a model does | auto_now, save() overrides, signals |
| Reference | every attribute and every key, in tables |
Start with A first model: it holds the ideas the other pages take for granted.