Django 6.1.3

14.3 Where Django leaks

Django 6.x shifts that trip up older mental models

Which architectural changes in Django 5.x and 6.1.3 invalidate habits learned from older tutorials and model training data?

Because most tutorials, Stack Overflow answers, and LLM training corpora are dominated by Django 3.x and 4.x code, engineers — and coding agents — routinely reach for patterns that Django 5.x and 6.1.3 have either replaced, hardened, or deprecated. Here is the source-verified field guide to the modern baseline we proved across Chapters 1 through 14.

Seven headline shifts in Django 6.1.3

  1. QuerySet.fetch_mode (FETCH_ONE, FETCH_PEERS, FETCH_RAISE, Chapter 7): Django 6.1 introduces pluggable lazy-loading modes in django/db/models/fetch_modes.py. FETCH_ONE preserves historical per-instance lazy loading; FETCH_PEERS tracks sibling instances hydrated in the same query via weak references ("_state.peers" on each model instance) and batches missing ForeignKey/deferred fields into one IN query automatically; and FETCH_RAISE raises FieldFetchBlocked on any accidental lazy query — giving teams native N+1 enforcement without third-party packages! (Remember from section 14.2: reverse ForeignKey managers still require prefetch_related.)
  2. Native django.tasks framework (Chapter 12): Django 6.0+ ships a first-class task abstraction — Task, TaskResult, TaskResultStatus, and TaskBackendHandler — configured via "settings.TASKS" with ImmediateBackend for development and DummyBackend for tests. Tasks must be module-level functions (validate_task), arguments and return values pass through normalize_json, exceptions under ImmediateBackend are captured into TaskError (status=FAILED) rather than raised out of enqueue(), and production code still must pair Task.enqueue with django.db.transaction.on_commit.
  3. Built-in ContentSecurityPolicyMiddleware and LazyNonce (Chapter 5): You no longer need third-party middleware to emit Content Security Policy headers or per-request script nonces. ContentSecurityPolicyMiddleware attaches a LazyNonce per request (only generating entropy if a template actually renders the nonce!) and builds "Content-Security-Policy" and "Content-Security-Policy-Report-Only" headers from "SECURE_CSP" / "SECURE_CSP_REPORT_ONLY".
  4. Composite primary keys (CompositePrimaryKey, Chapters 6 and 11): Django 5.2+ adds CompositePrimaryKey, allowing multi-column primary keys (pk = models.CompositePrimaryKey("tenant_id", "story_id")) resolved via CompositeAttribute tuples and tuple lookups (TupleExact, TupleIn). Note the boundary we saw in Chapter 11: AdminSite.register explicitly rejects models with "model._meta.is_composite_pk" (ImproperlyConfigured).
  5. SECRET_KEY_FALLBACKS for zero-downtime key rotation (Chapters 5 and 11): Signing (TimestampSigner), signed cookies, and AbstractBaseUser.get_session_auth_fallback_hash all verify incoming tokens against "SECRET_KEY_FALLBACKS" when the primary "SECRET_KEY" fails, then transparently re-sign/cycle sessions with the new primary key so rotating secrets no longer logs out every active user at once!
  6. Async ORM, "request.auser", and 1,500,000-iteration PBKDF2 (Chapters 3, 7, 11): Django 6.1 ships native async counterparts across QuerySets (aget, acount, afirst, asave, async for), sessions (aget, aset, aflush), auth (aauthenticate, alogin, alogout, "request.auser()"), and signals (asend with asyncio.TaskGroup). Meanwhile PBKDF2PasswordHasher defaults to 1,500,000 iterations (~488 ms per login check), backed by check_password_with_timing_attack_mitigation and PBKDF2PasswordHasher.harden_runtime.
  7. Database-level defaults (Field.db_default) & template partials (PartialTemplate, Chapters 6 and 10): Modern models can push defaults down into SQL DDL via db_default (DatabaseDefault) instead of Python-only default, and django/template/ natively supports inline named template fragments via PartialTemplate without splitting partials into separate HTML files.

Active RemovedInDjango2028Warning deprecations spotted in 6.1.3 source

While reading pinned Django 6.1.3 across this book, we hit five active deprecations slated for removal in Django 7.0 (RemovedInDjango2028Warning) that older code and generators still emit routinely:

Knowing both how Django’s layers connect and where Django 6.1 has moved gives you an immediate superpower when reviewing code — especially code written quickly by an AI coding agent. We close the book with a sharp six-question review protocol in Directing AI agents with a model of Django internals.