📝 Django

select_related and prefetch_related

P
Author
Pyland
📅
Published
30.06.2026
⏱️
Reading time
1 min
👁️
Views
73
🌳
Level
Advanced

Query optimization when working with related objects.

The N+1 Problem

# Bad: 1 query for tasks + N queries for each project
tasks = Task.objects.all()
for task in tasks:
    print(task.project.name)  # a new SQL query every time!
# Good: one query with JOIN
tasks = Task.objects.select_related('project', 'owner')
for task in tasks:
    print(task.project.name)  # already loaded
    print(task.owner.username)  # already loaded

Works with: ForeignKey, OneToOneField.

# Two queries: tasks + all tags
tasks = Task.objects.prefetch_related('tags')
for task in tasks:
    for tag in task.tags.all():  # already loaded
        print(tag.name)

Works with: ManyToManyField, reverse ForeignKey via related_name.

Combining Both

tasks = Task.objects.select_related('project', 'owner').prefetch_related('tags')

Prefetch with Filtering

from django.db.models import Prefetch

projects = Project.objects.prefetch_related(
    Prefetch(
        'tasks',
        queryset=Task.objects.filter(status='todo').order_by('priority'),
        to_attr='todo_tasks',  # store in attribute
    )
)

for project in projects:
    print(project.todo_tasks)  # list, not a queryset

When to Use Which

Situation Solution
task.project.name (FK) select_related('project')
task.tags.all() (M2M) prefetch_related('tags')
project.tasks.all() (reverse FK) prefetch_related('tasks')
FK + filter on related prefetch_related(Prefetch(...))

Your reaction to the article

💬 Comments (0)

🔐 Sign in to leave a comment
🚪 Login
💭

No comments yet

Be the first to share your opinion about this article!

🔗 Similar

Similar articles

Continue learning with these materials

📝

pytest-django: Testing Django

Охватываемые темы: Installation, @pytest.mark.djangodb, Fixtures, Testing views.

📅 30.06.2026 👁️ 138
📝

Django: Template Tags

Template tags are logic inside HTML. Unlike {{ variable }} which only outputs a value,...

📅 30.06.2026 👁️ 85
📝

Django: Static Files

Static files are CSS, JavaScript, images, and fonts. Django handles them in a specific way:...

📅 30.06.2026 👁️ 77

Did you like the article?

Subscribe to our updates and receive new articles first. Grow with PyLand!