File tree Expand file tree Collapse file tree 2 files changed +15
-2
lines changed
Expand file tree Collapse file tree 2 files changed +15
-2
lines changed Original file line number Diff line number Diff 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
Original file line number Diff line number Diff 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
633640Aggregating annotations
634641-----------------------
You can’t perform that action at this time.
0 commit comments