From 5adeb410253dcba2215dd8274255a6dd897f14d1 Mon Sep 17 00:00:00 2001 From: Tim Graham Date: Thu, 29 Oct 2015 17:38:05 -0400 Subject: [PATCH] [1.8.x] Fixed #24019 -- Fixed inaccurate docs about GenericRelation not supporting aggregation. This works at least as far back as Django 1.6 according to the test added in refs #10870. Backport of e19bd086d608c981098130a49e406de91dcc3d26 from master --- docs/ref/contrib/contenttypes.txt | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/docs/ref/contrib/contenttypes.txt b/docs/ref/contrib/contenttypes.txt index 16f1e9a6001..298606e8de2 100644 --- a/docs/ref/contrib/contenttypes.txt +++ b/docs/ref/contrib/contenttypes.txt @@ -475,18 +475,12 @@ signal. Generic relations and aggregation --------------------------------- -:doc:`Django's database aggregation API ` -doesn't work with a +:doc:`Django's database aggregation API ` works with a :class:`~django.contrib.contenttypes.fields.GenericRelation`. For example, you -might be tempted to try something like:: +can find out how many tags all the bookmarks have:: - Bookmark.objects.aggregate(Count('tags')) - -This will not work correctly, however. The generic relation adds extra filters -to the queryset to ensure the correct content type, but the -:meth:`~django.db.models.query.QuerySet.aggregate` method doesn't take them -into account. For now, if you need aggregates on generic relations, you'll -need to calculate them without using the aggregation API. + >>> Bookmark.objects.aggregate(Count('tags')) + {'tags__count': 3} .. module:: django.contrib.contenttypes.forms