Skip to content

Commit 7e765a6

Browse files
varunkasyappjacobtylerwalls
authored andcommitted
Fixed #36686 -- Clarified Meta.ordering is ignored in GROUP BY queries.
1 parent 3aba1fc commit 7e765a6

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

docs/ref/models/options.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,12 @@ not be looking at your Django code. For example::
312312

313313
ordering = [F("author").asc(nulls_last=True)]
314314

315+
.. admonition:: Default ordering and GROUP BY
316+
317+
In :ref:`GROUP BY queries <aggregation-ordering-interaction>` (for example,
318+
those using :meth:`~.QuerySet.values` and :meth:`~.QuerySet.annotate`), the
319+
default ordering is not applied.
320+
315321
.. warning::
316322

317323
Ordering is not a free operation. Each field you add to the ordering

docs/topics/db/aggregation.txt

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -627,8 +627,15 @@ fields you also select in a ``values()`` call.
627627
You might reasonably ask why Django doesn't remove the extraneous columns
628628
for you. The main reason is consistency with ``distinct()`` and other
629629
places: Django **never** removes ordering constraints that you have
630-
specified (and we can't change those other methods' behavior, as that
631-
would violate our :doc:`/misc/api-stability` policy).
630+
specified *explicitly with* ``order_by()`` (and we can't change those
631+
other methods' behavior, as that would violate our
632+
:doc:`/misc/api-stability` policy).
633+
634+
.. admonition:: Default ordering not applied to GROUP BY
635+
636+
``GROUP BY`` queries (for example, those using ``.values()`` and
637+
``.annotate()``) don't use the model's default ordering.
638+
Use ``order_by()`` explicitly when a given order is needed.
632639

633640
Aggregating annotations
634641
-----------------------

0 commit comments

Comments
 (0)