Skip to main content
Django Output Prediction Hard +35 XP

select_related Purpose

Why use select_related() here?

DjangoRead-only
posts = Post.objects.select_related('author').all()

What is the output?

Show answer and explanation

Answer: Fetches author in same SQL JOIN, avoiding N+1

select_related performs a SQL JOIN to fetch related objects in one query. Without it, accessing post.author for each post triggers N extra queries.

Hint: N+1 problem: one query per row is inefficient.

What this challenge tests

Output Prediction in Django: orm, n+1, select_related.

Related challenges