← Backend PythonQuestion 1 of 49
When do you use select_related() vs prefetch_related() in Django?
📘 Explanation
select_related() is used for forward ForeignKey/OneToOne relations and performs a SQL JOIN in one query, while prefetch_related() handles reverse ForeignKey and many-to-many relations using separate queries joined in Python. The wrong answers reverse these roles or invent caching backends like Redis that aren't actually involved.
Related questions
- What Django ORM feature lets you get "top N per group" (e.g. most expensive product per category) while handling ties…
- When is annotate() guaranteed to generate a GROUP BY in Django ORM?
- What are the standard methods in a Django REST Framework ViewSet and their corresponding HTTP methods?
- Examples of Python stdlib modules runnable as scripts (python -m ...):
- What are the first steps to debug performance issues in Django?
No login needed