summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKasyap Pentamaraju <vpentamaraju@webmd.net>2025-10-25 19:23:40 +0530
committerJacob Walls <jacobtylerwalls@gmail.com>2025-11-13 11:19:25 -0500
commitedec3e59a33ae4f456194c29630fd601213eee2f (patch)
tree621ecc6e893446793589b7d513ea39e005ccbf53
parentfea599d2a288112d566b798a48615dcd1c6e1b87 (diff)
[5.2.x] Fixed #36686 -- Clarified Meta.ordering is ignored in GROUP BY queries.
Backport of 7e765a68598b2b798e49bf1f4b431a7bcac085a4 from main.
-rw-r--r--docs/ref/models/options.txt6
-rw-r--r--docs/topics/db/aggregation.txt11
2 files changed, 15 insertions, 2 deletions
diff --git a/docs/ref/models/options.txt b/docs/ref/models/options.txt
index 3433d0730f..62064d4f24 100644
--- a/docs/ref/models/options.txt
+++ b/docs/ref/models/options.txt
@@ -309,6 +309,12 @@ not be looking at your Django code. For example::
ordering = [F("author").asc(nulls_last=True)]
+.. admonition:: Default ordering and GROUP BY
+
+ In :ref:`GROUP BY queries <aggregation-ordering-interaction>` (for example,
+ those using :meth:`~.QuerySet.values` and :meth:`~.QuerySet.annotate`), the
+ default ordering is not applied.
+
.. warning::
Ordering is not a free operation. Each field you add to the ordering
diff --git a/docs/topics/db/aggregation.txt b/docs/topics/db/aggregation.txt
index cd53f73c74..9598e54ca2 100644
--- a/docs/topics/db/aggregation.txt
+++ b/docs/topics/db/aggregation.txt
@@ -623,8 +623,15 @@ fields you also select in a ``values()`` call.
You might reasonably ask why Django doesn't remove the extraneous columns
for you. The main reason is consistency with ``distinct()`` and other
places: Django **never** removes ordering constraints that you have
- specified (and we can't change those other methods' behavior, as that
- would violate our :doc:`/misc/api-stability` policy).
+ specified *explicitly with* ``order_by()`` (and we can't change those
+ other methods' behavior, as that would violate our
+ :doc:`/misc/api-stability` policy).
+
+.. admonition:: Default ordering not applied to GROUP BY
+
+ ``GROUP BY`` queries (for example, those using ``.values()`` and
+ ``.annotate()``) don't use the model's default ordering.
+ Use ``order_by()`` explicitly when a given order is needed.
Aggregating annotations
-----------------------