Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

rind for Django developers

This page is a map. On the left, a Django or Django REST framework name you know; on the right, where the same idea lives in rind, and the chapter that explains it. Read it when you think “how do I write get_queryset here?”.

rind keeps most of Django’s ideas, but moves many decisions from run time to compile time, and makes explicit what Django does implicitly. The last section lists the main differences of spirit.

The project

DjangorindChapter
django-admin startprojectrind newStarting a project
manage.py startapprind add domainStarting a project
an appa domain: one crate per layer (<domain>-models, <domain>-api…)Organising an application
INSTALLED_APPS, ROOT_URLCONFapp(): .models(..), .api(..), .with(..)The command line
settings.pyenvironment variables (DATABASE_URL, BIND, …) and Rust code in app()The command line
manage.pyrind (the tool), and the application’s binaryThe command line
a management commandCli::command("name", "about", function)The command line
runserverrind serveThe command line
inspectdbrind inspectStarting from a database
middlewarea plugin’s hook (Around)Plugins
a reusable app (django-cors-headers, …)a plugin, .with(..), behind a Cargo featurePlugins

Models

DjangorindChapter
class Book(models.Model)#[derive(Model)] struct BookModels
Meta.db_table#[model(table = "…")] (always given)Models
CharField(max_length=200)String with #[column(max_length = 200)]Models
null=TrueOption<T>Models
default=…#[column(default = "SQL")], or a default in the inputModels, Inputs
ForeignKey(Author, on_delete=…)author_id: i64 with #[fk(Author, on_delete = …)]Models
related_name / book_setreverse = books on the key, or #[reverse(..)] on the target: never implicitModels
ManyToManyField#[m2m(name = …, through = "…", from = "…", to = "…")]Models
Meta.indexes, unique_together, constraints#[index(..)], #[unique(..)], #[check(..)]Models
an abstract model, a mixina field group (#[with_fields(..)]) or a behaviourBehaviours
a custom manager, objects.published()a scope: impl Book { fn published() -> Query<Book> }Models, Queries
a default manager that filters#[model(scoped)]: no default query at allModels
signals (post_save, …)none: a hook of the resource, a behaviour, or a function called by nameInputs, Behaviours

Queries

DjangorindChapter
Book.objects.filter(year__gt=1970)Query::<Book>::new().filter(Book::year().gt(1970))Queries
"author__name"Book::author().name(): a typed pathQueries
Q(a) | Q(b), ~Q(a)a | b, !a on conditionsQueries
F("price")the path itself, Book::price()Queries
.annotate(n=Count("books"))Author::books().count() in a serializer’s #[annotated], or in a queryQueries, Serializers
select_related, prefetch_relatednot needed: nested fields are loaded per page, in one query eachSerializers
.distinct() after a filter through a many relationnot needed: such a filter is an EXISTSQueries
select_for_update()a lock on the queryQueries
transaction.atomic()db.begin(); every write of a resource is already a transactionQueries, Inputs
makemigrations / migrate / showmigrationsrind make / rind migrate / rind statusMigrations
makemigrations --checkrind check (and the test rind new writes)Migrations
DATABASES, routers, .using()Db, replicas, ctx.database(name)Databases

The API

DRFrindChapter
ModelViewSet#[derive(Resource)]Resources
a router, urls.pyApi::new("domain").resource::<R>()Resources
ModelSerializer (reading)#[derive(Serializer)], the outputSerializers
ModelSerializer (writing), validate_<field>, validate#[derive(Input)], the inputInputs
read_only=True / write_only=Truea field only in the output / only in the inputSerializers
source="author_id"#[field(name = "author")] author_idSerializers
a nested serializer#[nested(key = …)], #[nested(many, by = …)]Serializers
SerializerMethodField#[computed(loader = …)], called once per pageSerializers
dynamic fields (?fields=)built in: fields, omit, includeSerializers
perform_create, perform_update, save()a hook: impl Writes for RInputs
django-filter’s FilterSet#[derive(FilterSet)]Filters
search_fields#[search(path, mode)]Filters
ordering_fields, Meta.ordering#[ordering(by(..), default(..))]Filters
PageNumberPaginationbuilt in; {parameters, nav, results}Filters
@action(detail=True)#[action(POST)] taking the objectActions
@action(detail=False)#[action(GET)] without the objectActions
query parameters read by handtyped parameters, #[derive(Parameters)]Parameters
authentication classesan Authenticate implementation, or rind-authAuthentication
djangorestframework-simplejwtrind-auth: POST /token, /token/refreshSigning in
request.userctx.user, an IdentityAuthentication
permission_classes#[rule(..)]Permissions
get_queryset filtering on the usera mode: #[mode(.., query = …)]Permissions
has_object_permissionthe mode’s query: an object outside it is a 404Permissions
drf-spectacularbuilt in: rind openapiOpenAPI
renderers (CSV, xlsx)?format=csv, ?format=xlsxExports
APIClient in testsapp.router(..) called with tower’s oneshotTutorial, part 6

Around the API

DjangorindChapter
django.core.mail, anymailthe mail pluginMail
Celery, django-qthe jobs pluginJobs
FileField, django-storagesFile columns and the storage pluginFiles
ImageField, easy-thumbnailsthe images pluginImages
the cache framework, cache_pagethe cache plugin, with ETagsCache
LOGGINGtracing, RUST_LOG, RIND_LOG=jsonLogging
django-cors-headersthe CORS settingsCORS
django-prometheusthe metrics pluginMetrics

What is not there

Some things a Django developer expects do not exist, on purpose or not yet:

  • No admin. rind serves APIs only.
  • No templates, no forms, no sessions. The front end is a separate client.
  • No createsuperuser. Write a command of your own: the tutorial shows one.
  • No migrating backwards, no squashing. Migrations go forward; a mistake is fixed by a new migration.

Differences of spirit

  • Errors at compile time. A misspelt field, a relation followed the wrong way, a rule naming an action that does not exist: the compiler refuses them, where Django would fail at run time, or silently do nothing.
  • Nothing implicit. No reverse relation is guessed, no manager filters behind your back, no signal runs code from far away. What a write does is in its hook, or in the behaviours listed on the model.
  • No N+1 by construction. Nested objects and computed values are loaded for a whole page at a time. A list reads only the columns of the fields it sends.
  • Pages that never overlap. Every ordering ends with the primary key.
  • SQL you can read. Migrations are plain SQL files, and queries are built from typed paths that turn into predictable SQL.