Opened 13 years ago

Closed 13 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

Description

The example lookup in https://docs.djangoproject.com/en/1.3/topics/db/aggregation/#order-of-annotate-and-filter-clauses
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 by Ramiro Morales, 13 years ago

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 https://docs.djangoproject.com/en/1.3/topics/db/queries/#lookups-that-span-relationships

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