Part I · The request
Chapter 2: Booting Django
The promiseJust import your models and go.
The machineryLazy configuration, the two-phase app registry, and why import order decides whether a project starts cleanly or crashes before the first request.
Inside a running Django view or management command, configuration and models feel as ambient as Python builtins. Any file can import settings, read a flag, and import any model from any app without asking who loaded them or in what order. Step outside an already-booted worker — write a standalone script, drop a model import into an app’s __init__.py, query a table during startup, or assign directly to settings in a test — and the illusion breaks at once into ImproperlyConfigured, AppRegistryNotReady, OperationalError, or silently stale caches.
Everything that turns an ordinary Python interpreter into a booted Django process fits inside a seventeen-line function, django.setup. On our modest eight-app newsroom project, running django.setup cold across five fresh Python processes takes a median of 312. Almost nobody guesses where that third of a second actually goes.
Where the 313 milliseconds go
ch02-bench). Nearly forty percent of django.setup is spent importing django.urls on line 16 before logging or the app registry even starts; loading "desk.settings" itself takes less than three milliseconds.Look at the two ends of that bar. Parsing our settings module — copying 150 uppercase defaults from django and overlaying 20 explicit project settings — takes just 2, less than one percent of startup. Meanwhile, a single lazy from django inside django.setup pulls in Django’s URL, HTTP, and internationalization modules for the first time and consumes 124 (39.9%) before a single application is discovered.
Then Apps.populate takes over, spending 71.21 ms discovering and constructing eight AppConfig objects, 73.74 ms importing models and registering eighteen model classes (15 explicit models plus 3 auto-created many-to-many join tables), and 14.05 ms running every AppConfig.ready hook — including AdminConfig scanning every installed app for admin registrations.
Why boot is split into strict phases
Django pays that orchestration complexity to solve two problems that would otherwise deadlock every non-trivial project.
First, dozens of Django’s own modules run from django at import time, long before a process has configured "DJANGO_SETTINGS_MODULE" or called LazySettings. Django therefore ships settings as an unevaluated LazyObject shell that stays completely inert until the very first attribute read, caches resolved keys onto its instance __dict__, and still pays a measurable pure-Python __getattribute__ tax on every lookup (236.4 ns versus 17.0 ns for a plain object attribute).
Second, Python executes a model’s ModelBase metaclass at the instant its class statement runs — and that metaclass needs to know which installed application owns the model, while cross-app foreign keys routinely point at models whose modules have not been imported yet. Apps.populate breaks both cycles by forbidding model imports until all AppConfig instances exist (Phase 1), queuing unresolved cross-app foreign keys as curried callbacks in _pending_operations while models import (Phase 2), and withholding AppConfig.ready until every model in the project is registered (Phase 3).
That design makes ordinary Django apps feel effortless — until code violates the phase order, runs database queries before migrations exist, or mutates settings after startup without notifying the subsystems that cached old values. We start at the very first line of boot, with the proxy that makes importing settings safe at any time, in Lazy settings and configuration.