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
- id: C1 claim: prefetch_related retrieves related objects in a single batch after the primary query. source: S1 evidence: "automatically retrieve, in a single batch, related objects"
- id: C2 claim: Unlike select_related, prefetch_related does a separate lookup and joins in Python. source: S1 evidence: "does a separate lookup for each relationship"
- id: C3 claim: SQLite's query planner decides how to compute a declarative SQL query. source: S2 evidence: "it is a declarative language, not a procedural language"
Sources
- [S1] QuerySet API reference. Django docs. https://docs.djangoproject.com/en/5.2/ref/models/querysets/ . License: BSD-3-Clause. Accessed 2026-11-19.
- [S2] Query Planning. SQLite. https://www.sqlite.org/queryplanner.html . License: Public-Domain. Accessed 2026-11-19.
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
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.},
}
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.
- django-prefetch-related.table.json (table; CC-BY-4.0)
- django-prefetch-related.schema.json (schema; CC-BY-4.0)
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.