1
0
mirror of https://github.com/django/django.git synced 2025-07-19 09:09:13 +00:00

[1.8.x] Refs #25136 -- Fixed nonexistent field reference in aggregation topic guide.

Thanks Ankush Thakur for the report and Simon for the review.

Backport of fe70f280d7ea4402f676696c4013c4a23d4e4990 from master
This commit is contained in:
Tim Graham 2016-04-27 14:49:29 -04:00
parent f8b8bcea5d
commit 7934695a0f

View File

@ -194,24 +194,25 @@ results <https://code.djangoproject.com/ticket/10060>`_, as multiple tables are
cross joined. Due to the use of ``LEFT OUTER JOIN``, duplicate records will be cross joined. Due to the use of ``LEFT OUTER JOIN``, duplicate records will be
generated if some of the joined tables contain more records than the others: generated if some of the joined tables contain more records than the others:
>>> Book.objects.first().authors.count() >>> book = Book.objects.first()
>>> book.authors.count()
2 2
>>> Book.objects.first().chapters.count() >>> book.store_set.count()
3 3
>>> q = Book.objects.annotate(Count('authors'), Count('chapters')) >>> q = Book.objects.annotate(Count('authors'), Count('store'))
>>> q[0].authors__count >>> q[0].authors__count
6 6
>>> q[0].chapters__count >>> q[0].store__count
6 6
For most aggregates, there is no way to avoid this problem, however, the For most aggregates, there is no way to avoid this problem, however, the
:class:`~django.db.models.Count` aggregate has a ``distinct`` parameter that :class:`~django.db.models.Count` aggregate has a ``distinct`` parameter that
may help: may help:
>>> q = Book.objects.annotate(Count('authors', distinct=True), Count('chapters', distinct=True)) >>> q = Book.objects.annotate(Count('authors', distinct=True), Count('store', distinct=True))
>>> q[0].authors__count >>> q[0].authors__count
2 2
>>> q[0].chapters__count >>> q[0].store__count
3 3
.. admonition:: If in doubt, inspect the SQL query! .. admonition:: If in doubt, inspect the SQL query!