summaryrefslogtreecommitdiff
path: root/docs
diff options
context:
space:
mode:
authorTim Graham <timograham@gmail.com>2016-04-27 14:49:29 -0400
committerTim Graham <timograham@gmail.com>2016-04-27 15:26:10 -0400
commitcc5c4ae35de2170fc0b597a9c6acb941b91941ab (patch)
tree3fed845874d11d82876cb74afd6c488fadbc94dc /docs
parent3f93f8d8e38612b326f616cce518f71d06a157eb (diff)
[1.9.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
Diffstat (limited to 'docs')
-rw-r--r--docs/topics/db/aggregation.txt13
1 files changed, 7 insertions, 6 deletions
diff --git a/docs/topics/db/aggregation.txt b/docs/topics/db/aggregation.txt
index c5dddf29c6..ddae76a442 100644
--- a/docs/topics/db/aggregation.txt
+++ b/docs/topics/db/aggregation.txt
@@ -193,24 +193,25 @@ Combining multiple aggregations with ``annotate()`` will `yield the wrong
results <https://code.djangoproject.com/ticket/10060>`_ because joins are used
instead of subqueries:
- >>> Book.objects.first().authors.count()
+ >>> book = Book.objects.first()
+ >>> book.authors.count()
2
- >>> Book.objects.first().chapters.count()
+ >>> book.store_set.count()
3
- >>> q = Book.objects.annotate(Count('authors'), Count('chapters'))
+ >>> q = Book.objects.annotate(Count('authors'), Count('store'))
>>> q[0].authors__count
6
- >>> q[0].chapters__count
+ >>> q[0].store__count
6
For most aggregates, there is no way to avoid this problem, however, the
:class:`~django.db.models.Count` aggregate has a ``distinct`` parameter that
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
2
- >>> q[0].chapters__count
+ >>> q[0].store__count
3
.. admonition:: If in doubt, inspect the SQL query!