Defining models
Declare a table with the Model derive and generate a migration.
The Model derive
#[derive(Model)] maps a struct to a table and emits a typed queryset via Model::objects(). Field attributes set column constraints.
#[derive(Model)]
#[rustango(table = "author", display = "name")]
pub struct Author {
#[rustango(primary_key)]
pub id: Auto<i64>,
#[rustango(max_length = 120, index)]
pub name: String,
#[rustango(auto_now_add)]
pub created_at: Auto<chrono::DateTime<chrono::Utc>>,
}
Changed in 0.41
Querysets gained select_related / prefetch_related for N+1-free relation loading.
Generate migrations
After editing a model run cargo run -- makemigrations; the diff is written as a JSON snapshot you commit alongside the code.
Foreign keys
Declare a relation with #[rustango(fk = "...", on = "id")] and load it eagerly with select_related.