prefetch_related() fetches the entire related set by default — including records you will never use. When you only need a subset — say, only the published books of each author — use Prefetch() with a custom queryset so the database filters up front, instead of loading everything and filtering in Python. This works for ManyToMany relations and reverse relations, whenever you know the filter condition when writing the query.

The Problem

Suppose an author list page only shows published books. The familiar approach is to prefetch everything, then filter in Python:

authors = Author.objects.prefetch_related("books")
for author in authors:
published = [b for b in author.books.all() if b.is_published]

This code is correct but wasteful: every draft book is still downloaded, eating RAM and bandwidth only to be thrown away. Worse, if you forget prefetch_related, every touch of author.books.all() fires another query — exactly the N+1 problem prefetching exists to solve. The issue is not the filtering; it is that the excess data already reached Python before being discarded.

The Solution

Use Prefetch() to attach the filter condition directly to the prefetch query:

from django.db.models import Prefetch
authors = Author.objects.prefetch_related(
Prefetch(
"books",
queryset=Book.objects.filter(is_published=True).order_by("-published_at"),
to_attr="published_books",
)
)
for author in authors:
for book in author.published_books:
print(author.name, book.title)

The whole job costs just two queries: one for the authors, one for exactly the published books of all authors. to_attr="published_books" stores the result as a ready-made list on each author, so the loop triggers no extra queries. Two things to remember: first, published_books is a plain Python list, not a queryset, so you cannot call .filter() on it; second, author.books.all() is a separate cache and will query again if you touch it — so pick a clear to_attr name and use it consistently in both views and templates.

Apply It to Your Project

Review the places where you prefetch and then filter in Python: a profile page that only shows delivered orders, a dashboard that only counts approved comments, a digest email that only pulls this week’s posts. Each one is a candidate for conditional Prefetch(). You can also nest several Prefetch() objects in a single call, and combine select_related() inside the child queryset when needed.

Related Posts

FAQ

How is Prefetch() different from prefetch_related(“books”)?

prefetch_related("books") is the shorthand that fetches the whole relation. Prefetch() lets you specify your own queryset — filtering, ordering, even select_related() inside it — and names the attribute that receives the result via to_attr.

Can I keep filtering after using to_attr?

Not queryset-style, because to_attr stores a plain list. If you need further filtering on a dynamic condition, filter that list in Python, or write another Prefetch() with the right condition from the start.

Can Prefetch() be used for ForeignKey?

No need. Forward ForeignKey and OneToOne relations use select_related(). Prefetch() is for ManyToMany and reverse relations, where Django must run a separate query.

When should you NOT use conditional Prefetch()?

When the filter condition depends on each parent object in a way a single shared queryset cannot express, or when you genuinely need the whole relation in several places. Then prefetching everything once and reusing it is still cheaper than prefetching separate subsets.

💬 Have you tried this in your own project? If you hit an error or found a neater way, leave a comment below — I read them all and will fold the best ideas into the post.

References