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.

PageWhat it coversIn Django
A first modelone model line by line, its table, what the derive gives, from the struct to the tableclass Train(models.Model)
Fields and column typeswhich Rust type gives which SQL type, NULL, every type rind knowsthe field classes: CharField, IntegerField, JSONField…
Column optionslengths, defaults, unique columns, indexed columns, generated columnsmax_length, default, unique, db_index
Types of your ownstoring an enum or any type of yours in a columncustom fields, choices
Primary keysid, another key, keys the application givesprimary_key=True, AutoField
Foreign keyspointing to another model, what a deletion doesForeignKey, on_delete
Reverse and many-to-many relationswalking a key backwards, link tablesrelated_name, ManyToManyField
Treesrows that have a parent among themselvesdjango-mptt, django-treebeard
Indexes and constraintsindexes on several columns, unique together, checksMeta.indexes, Meta.constraints
Scopes and scoped modelsnamed queries on a model; models with no default querymanagers, objects.published()
Sharing fieldsfields written once, used by several modelsabstract models, mixins
Writing objectsinsert, save, update, delete on an objectobj.save(), obj.delete()
Behaviours and hookswhat every write of a model doesauto_now, save() overrides, signals
Referenceevery attribute and every key, in tables

Start with A first model: it holds the ideas the other pages take for granted.