Andrew Gorcester
Andrew Gorcester

Reputation: 19973

Filtering related fields in django's annotate() feature

The Django documentation gives examples for using annotate() to produce aggregate results based on related fields of a QuerySet (i.e., using joins in annotate()).

A simplified example from the docs is for instance Store.objects.annotate(min_price=Min('books__price')), where books is a ManyToMany field of Store to Book and price is a field of Book.

To continue this example, how would I generate an annotated QuerySet of Store objects with the lowest prices not for all books in the store, but just for books with "author='William Shakespeare'"? In other words, how do I filter the related fields used to calculate the aggregate?

Upvotes: 3

Views: 1603

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599540

The documentation explains how to do this:

Store.objects.filter(books__author='William Shakespeare').annotate(
                     min_price=Min('books__price'))

As that link notes, the order of filter and annotate matters here - because you want to only count books that match the filter in the annotation, the filter must come first.

Upvotes: 2

Related Questions