Opened 6 years ago

Closed 6 years ago

#17748 closed Bug (invalid)

Wrong lookup in the aggregation examples

Reported by: yliharma Owned by: nobody
Component: Documentation Version: 1.3
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


The example lookup in
is wrong:

>>> Publisher.objects.annotate(num_books=Count('book')).filter(book__rating__gt=3.0)
and the query:

>>> Publisher.objects.filter(book__rating__gt=3.0).annotate(num_books=Count('book'))

because the model Publisher doesn't contain any ForeignKey to the model Book (while the reverse is True).
It should be:

>>> Publisher.objects.annotate(num_books=Count('book')).filter(book_set__rating__gt=3.0)
and the query:

>>> Publisher.objects.filter(book_set__rating__gt=3.0).annotate(num_books=Count('book'))

Change History (1)

comment:1 Changed 6 years ago by Ramiro Morales

Resolution: invalid
Status: newclosed

No, the parts separated by '__' of a lookup that spans relationships use the lowercase model names, not the <model>_set notation. See

Note: See TracTickets for help on using tickets.
Back to Top