← All notes

Prefetch related objects in a Django QuerySet without a large join

Answer: prefetch_related() returns a QuerySet that retrieves related objects for the specified lookups, in a single batch. It does a separate lookup for each relationship, and does the joining in Python. That avoids the much larger result set that would result from joining across a many relationship.

prefetch_related versus select_related

select_related is limited to single-valued relationships: foreign key and one-to-one. It gets the related objects in the same database query. prefetch_related is the tool for many-valued relationships. We can reduce to just two queries using prefetch_related. The additional queries in prefetch_related() run after the primary query has been executed.

What the query planner still does

SQL is a declarative language, not a procedural language. The task of figuring out the how is delegated to the query planner subsystem. select_related gets the related objects in the same database query. prefetch_related does a separate lookup for each relationship instead.

| prefetch_related | select_related | |---|---| | prefetch_related | Separate lookup per relationship, joining in Python | | select_related | Same database query, single-valued relationships | | foreign key | One-to-one and foreign key can use select_related | | query planner | Decides how to compute declarative SQL |

See also

Claims

Sources

License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Django docs, QuerySet API reference; SQLite, Query Planning. BSD-3-Clause; Public-Domain. Text was condensed and changed from the originals.

License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: Django docs, QuerySet API reference; SQLite, Query Planning. BSD-3-Clause; Public-Domain.

Cite this

Permanent ID: iil:note:scnj7z96

iinventedlight. 2026. "Prefetch related objects in a Django QuerySet without a large join." iinventedlight Library, iil:note:scnj7z96, version of 2026-11-19. https://iinventedlight.com/library/notes/django-prefetch-related/. License: CC-BY-4.0.

@misc{iil_note_scnj7z96,
  author = {{iinventedlight}},
  title = {{Prefetch related objects in a Django QuerySet without a large join}},
  howpublished = {iinventedlight Library},
  year = {2026},
  date = {2026-11-19},
  url = {https://iinventedlight.com/library/notes/django-prefetch-related/},
  note = {Permanent ID iil:note:scnj7z96. License: CC-BY-4.0.},
}

Markdown

Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).

Extra files beside this note

These files use this note's license, shown below. Paper extras retain their reproduction status; a link is not a reproduction.

All artifact files (JSON index)

For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/django-prefetch-related.md.