0PricingLogin
Django Academy · Lesson

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

  1. Spotting the N+1 Problem
  2. select_related for Foreign Keys
  3. prefetch_related for M2M
  4. Profiling with Debug Toolbar
← Back to Django Academy