---
source: https://iinventedlight.com/library/notes/django-prefetch-related.md
title: "Prefetch related objects in a Django QuerySet without a large join"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "BSD-3-Clause (Django docs); Public-Domain (SQLite); prose restates cited source lines in own words"
license_family: mixed-green
attribution: "Summary by iinventedlight (AI-assisted). Sources: Django docs, QuerySet API reference; SQLite, Query Planning. BSD-3-Clause; Public-Domain."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-19
status: published
approved_by: "Cole Withington, 2026-11-20 13:14 PT: \"Publish the full batch and rewrite the 6 live notes\""
checked_on: 2026-11-19
as_of: 2026-11-19
confidence: medium
untested: "Not reproduced (2026-11-19). Official docs and source files were read; no test suite, server, or API call was run for this note."
category: coding-references
topic_area: "code-python: Django prefetch_related and the SQLite query planner"
tags: [django, queryset, prefetch, sqlite, orm]
client: Librarian
sources:
  - id: S1
    title: "QuerySet API reference"
    publisher: Django docs
    url: https://docs.djangoproject.com/en/5.2/ref/models/querysets/
    accessed: 2026-11-19
    license: "BSD-3-Clause"
  - id: S2
    title: "Query Planning"
    publisher: SQLite
    url: https://www.sqlite.org/queryplanner.html
    accessed: 2026-11-19
    license: "Public-Domain"
---
# 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

- [django-querysets](https://iinventedlight.com/library/notes/django-querysets/)
- [django-models](https://iinventedlight.com/library/notes/django-models/)
- [sqlite-query-planner-indexes](https://iinventedlight.com/library/notes/sqlite-query-planner-indexes/)

## 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.

---

Agents: [/agents.md](/agents.md) | [/.well-known/agent-library.json](/.well-known/agent-library.json).
