Django ORM
Overview
The Django ORM ships with the Django web framework and is the single, definitive source of truth for your data: each model maps to a database table, and you query through lazy QuerySets. It includes a migration system and integrates with the Django admin. Read this when building data access inside a Django project rather than as a standalone library (the ORM is not distributed separately).
Official sources
Core concepts
- Models: subclasses of
django.db.models.Model; each field maps to a column and each model to a table. - QuerySets: lazy, chainable query objects (
Model.objects.filter(...)) that hit the database only when evaluated. - Managers: the
objectsmanager is the query entry point; custom managers add reusable query logic. - Migrations: schema changes are captured by
makemigrationsand applied withmigrate. - Relationships:
ForeignKey,ManyToManyField,OneToOneFieldmodel associations with related-object access. - Aggregation & raw SQL:
annotate/aggregatefor grouped queries, plus escape hatches for raw SQL when needed.
Best practices
- Use
select_related(joins) andprefetch_related(separate queries) to avoid N+1 when traversing relations (per docs). - Always create and review migrations with
makemigrations/migrate; keep them in version control. - Defer or restrict columns with
only()/defer()andvalues()when you do not need full model instances. - Wrap multi-step writes in
transaction.atomic()for atomicity.
Common pitfalls
- Iterating related objects in a loop without
select_related/prefetch_related→ N+1 query storms. - Assuming QuerySets execute immediately → they are lazy and re-evaluating one repeatedly re-runs the query; cache with
list()when reused.
Examples
from django.db import models
class User(models.Model):
name = models.CharField(max_length=50)
# querying
ada = User.objects.create(name="Ada")
adults = User.objects.filter(name="Ada").select_related()
Further reading
- https://docs.djangoproject.com/en/stable/topics/db/queries/ — making queries
- https://docs.djangoproject.com/en/stable/topics/db/optimization/ — query optimization
Related skills
- ../sqlalchemy — standalone Python ORM/toolkit alternative
- ../prisma — type-safe ORM in the TS ecosystem