Technoarch Softwares - Django 6.1 Fetch Modes: Fix N+1 Queries in One Line

Django 6.1 Fetch Modes: Fix N+1 Queries in One Line

A few weeks ago I was looking at a slow page in one of our Django projects. Nothing looked wrong in the code. The view was short, the template was simple. Then I opened the debug toolbar and saw hundreds of queries for a single page load.

If you have worked with Django for a while, you know where this is going. It was the N+1 problem again.

The good news is that Django 6.1, released on August 5, 2026, has a new feature that makes this much easier to deal with. It's called fetch modes, and I think not enough people know about it yet.

First, a quick reminder of the problem

Say you have books and authors:

class Author(models.Model):
    name = models.CharField(max_length=100)

class Book(models.Model):
    title = models.CharField(max_length=200)
    author = models.ForeignKey(Author, on_delete=models.CASCADE)

And you write this loop:

books = Book.objects.all()
for book in books:
    print(book.author.name)

It looks fine, but Django runs one query for the books, and then one extra query for every book to get its author. 100 books means 101 queries. 10,000 books means 10,001.

The usual fix is select_related() or prefetch_related(). They work, but you have to remember them every time. And they are easy to forget, especially when a template or serializer reads a related field that you did not think about.

The new way: FETCH_PEERS

With Django 6.1 you can do this instead:

from django.db import models

books = Book.objects.fetch_mode(models.FETCH_PEERS)
for book in books:
    print(book.author.name)

Same loop, but now Django only runs two queries. One for the books, and one for all the authors together.

Here is what happens. When your code asks for book.author the first time, Django sees that the other books from the same QuerySet will need it too, so it loads the authors for all of them at once. You don't need to list the fields in advance.

Before and after FETCH_PEERS: 101 queries reduced to 2

There are three modes

Mode What it does
FETCH_ONE The default. Loads the missing field for one object only, just like before.
FETCH_PEERS Loads the missing field for every object from the same QuerySet.
FETCH_RAISE Raises an error instead of running a query.

Your old code keeps working because FETCH_ONE is the default. You can try the new modes on one slow page at a time.

FETCH_RAISE is great for tests

This one is my favorite, to be honest. Instead of loading extra data, it stops you:

from django.core.exceptions import FieldFetchBlocked

books = Book.objects.fetch_mode(models.FETCH_RAISE)

try:
    for book in books:
        print(book.author.name)
except FieldFetchBlocked:
    print("A hidden query was about to run")

If you use it in tests for your important pages, you will find out right away when someone adds a change that triggers extra queries. Much better than finding out from a user who says the site feels slow.

A few things to know before you try it

You need Django 6.1 or newer, so this won't work on older projects until you upgrade.

Also, FETCH_PEERS is not always the fastest option. It uses two queries, while select_related() can get the same data in one query with a JOIN. So check the numbers on your own pages before you decide.

One more thing: calling select_related() with no arguments is now deprecated. Django suggests naming the fields you want, or using FETCH_PEERS.

Final thoughts

I like this feature because it removes a mistake that every Django developer makes sooner or later. One line can turn hundreds of queries into two, and FETCH_RAISE helps you catch the next one early.

If you are thinking about upgrading to 6.1, start with your slowest list page. Try FETCH_PEERS, check the query count, and see the difference yourself.

0 Comments:

Leave a Comments

Your email address will not be published. Required fields are marked *