prefetch_related for M2M
Batch-load many-to-many data efficiently.
Meet prefetch_related
prefetch_related handles the relationships a join cannot, like many-to-many and reverse links, with smart batching. 📦
Two Queries, Not Many
Instead of one giant join, it runs a second query for the related rows, then matches them up in Python.
All lessons in this course
- Spotting the N+1 Problem
- select_related for Foreign Keys
- prefetch_related for M2M
- Profiling with Debug Toolbar