From fcd44b889f36c4be87910745614a0a4c88d7a3d8 Mon Sep 17 00:00:00 2001 From: Mariusz Felisiak Date: Tue, 8 Jun 2021 16:39:00 +0200 Subject: [PATCH] Refs #14357 -- Updated docs about interaction between aggregations and QuerySet.order_by(). Obsolete since 0ddb4ebf7bfcc4730c80a772dd146a49ef6895f6. --- docs/topics/db/aggregation.txt | 34 +++++++++++++++------------------- 1 file changed, 15 insertions(+), 19 deletions(-) diff --git a/docs/topics/db/aggregation.txt b/docs/topics/db/aggregation.txt index 2e882e2a1a..772de0abdb 100644 --- a/docs/topics/db/aggregation.txt +++ b/docs/topics/db/aggregation.txt @@ -505,15 +505,14 @@ include the aggregate column. .. _aggregation-ordering-interaction: -Interaction with default ordering or ``order_by()`` -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +Interaction with ``order_by()`` +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -Fields that are mentioned in the ``order_by()`` part of a queryset (or which -are used in the default ordering on a model) are used when selecting the -output data, even if they are not otherwise specified in the ``values()`` -call. These extra fields are used to group "like" results together and they -can make otherwise identical result rows appear to be separate. This shows up, -particularly, when counting things. +Fields that are mentioned in the ``order_by()`` part of a queryset are used +when selecting the output data, even if they are not otherwise specified in the +``values()`` call. These extra fields are used to group "like" results together +and they can make otherwise identical result rows appear to be separate. This +shows up, particularly, when counting things. By way of example, suppose you have a model like this:: @@ -523,23 +522,20 @@ By way of example, suppose you have a model like this:: name = models.CharField(max_length=10) data = models.IntegerField() - class Meta: - ordering = ["name"] - -The important part here is the default ordering on the ``name`` field. If you -want to count how many times each distinct ``data`` value appears, you might -try this:: +If you want to count how many times each distinct ``data`` value appears in an +ordered queryset, you might try this:: + items = Item.objects.order_by('name') # Warning: not quite correct! - Item.objects.values("data").annotate(Count("id")) + items.values('data').annotate(Count('id')) ...which will group the ``Item`` objects by their common ``data`` values and then count the number of ``id`` values in each group. Except that it won't -quite work. The default ordering by ``name`` will also play a part in the -grouping, so this query will group by distinct ``(data, name)`` pairs, which -isn't what you want. Instead, you should construct this queryset:: +quite work. The ordering by ``name`` will also play a part in the grouping, so +this query will group by distinct ``(data, name)`` pairs, which isn't what you +want. Instead, you should construct this queryset:: - Item.objects.values("data").annotate(Count("id")).order_by() + items.values('data').annotate(Count('id')).order_by() ...clearing any ordering in the query. You could also order by, say, ``data`` without any harmful effects, since that is already playing a role in the