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.
This commit is contained in:
Tim Graham 2015-10-29 17:38:05 -04:00
parent 81b55fb900
commit e19bd086d6
1 changed files with 4 additions and 10 deletions

View File

@ -450,18 +450,12 @@ signal.
Generic relations and aggregation Generic relations and aggregation
--------------------------------- ---------------------------------
:doc:`Django's database aggregation API </topics/db/aggregation>` :doc:`Django's database aggregation API </topics/db/aggregation>` works with a
doesn't work with a
:class:`~django.contrib.contenttypes.fields.GenericRelation`. For example, you :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')) >>> Bookmark.objects.aggregate(Count('tags'))
{'tags__count': 3}
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.
.. module:: django.contrib.contenttypes.forms .. module:: django.contrib.contenttypes.forms