Auto-prefetching with model field fetch modes in Django 6.1
summary
Django 6.1 introduces model field fetch modes that change how deferred fields load: fetch_peers auto-prefetches single-valued relations (acts like on-demand prefetch_related and cascades down single-value chains), fetch_raise surfaces N+1 as exceptions, and fetch_1 preserves the old serial fetch. Configure modes per model via managers or call .fetch_mode on a QuerySet; you can write custom modes or fallbacks that warn then fetch_1. Tradeoffs: it can amplify existing over‑fetching, break assumptions about serial updates, and widen race windows; measure impact with Django Debug Toolbar or the queries test helper.
AI-written summary · about
chapters (14)
- 0:00Introduction and speaker background
- 1:13N+1 queries problem explained
- 5:25Existing ORM helpers: select_related and prefetch_related
- 7:00Introducing fetch modes in Django 6.1
- 8:25Fetch modes: peers, raise, and default behavior
- 9:30Use cases and limitations of fetch peers
- 12:28History and development of fetch modes
- 13:54When not to use fetch peers
- 15:53Fetch raise mode for debugging and production
- 16:18Avoiding repetition with model managers
- 17:28Global settings and future of fetch modes
- 18:35Extending fetch modes and custom behavior
- 19:41Call to action and community involvement
- 20:03Q&A session
