← All notes

select_related joins foreign keys in one query; prefetch_related uses extra queries

Answer: select_related() returns a QuerySet that joins named foreign-key relationships and selects the extra related objects when that query runs. Used this way, it can boost performance by fetching data early, instead of triggering on-demand loading. prefetch_related does a separate lookup for each relationship and does the joining in Python.

When to use which

Because a join across a many-valued relation would explode the result set, select_related is limited to foreign key and one-to-one. prefetch_related can also follow many-to-many and many-to-one relations. You can follow foreign keys with a chain such as author__hometown. Pass None to clear related fields added by earlier select_related calls.

Prefetching helps when you already know you will touch related objects later, so you can avoid a query per object. Those extra prefetch_related() queries run after the QuerySet starts evaluating and after the primary query has finished.

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 (select_related and prefetch_related), BSD-3-Clause (https://raw.githubusercontent.com/django/django/main/LICENSE). Copyright (c) Django Software Foundation and individual contributors; django/django (GitHub), querysets.txt (Django source docs), BSD-3-Clause (https://raw.githubusercontent.com/django/django/main/LICENSE). Copyright (c) Django Software Foundation and individual contributors. Text was condensed and changed from the originals.

License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: Django docs, QuerySet API reference (select_related and prefetch_related), BSD-3-Clause (https://raw.githubusercontent.com/django/django/main/LICENSE). Copyright (c) Django Software Foundation and individual contributors; django/django (GitHub), querysets.txt (Django source docs), BSD-3-Clause (https://raw.githubusercontent.com/django/django/main/LICENSE). Copyright (c) Django Software Foundation and individual contributors.

Cite this

Permanent ID: iil:note:va9gn0qj

iinventedlight. 2026. "select_related joins foreign keys in one query; prefetch_related uses extra queries." iinventedlight Library, iil:note:va9gn0qj, version of 2026-11-17. https://iinventedlight.com/library/notes/django-select-related/. License: CC-BY-4.0.

@misc{iil_note_va9gn0qj,
  author = {{iinventedlight}},
  title = {{select\_related joins foreign keys in one query; prefetch\_related uses extra queries}},
  howpublished = {iinventedlight Library},
  year = {2026},
  date = {2026-11-17},
  url = {https://iinventedlight.com/library/notes/django-select-related/},
  note = {Permanent ID iil:note:va9gn0qj. 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).

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