From 7cf1c22d4dfdd46f2082cfc55b714b68c4fd2de3 Mon Sep 17 00:00:00 2001 From: Jacob Walls Date: Mon, 9 Feb 2026 15:09:56 -0500 Subject: Refs #36644 -- Documented no pk ordering in first()/last() after empty order_by(). --- docs/ref/models/querysets.txt | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) (limited to 'docs/ref') diff --git a/docs/ref/models/querysets.txt b/docs/ref/models/querysets.txt index 890395f6de..c819015b25 100644 --- a/docs/ref/models/querysets.txt +++ b/docs/ref/models/querysets.txt @@ -2774,8 +2774,9 @@ the direction is changed. *Asynchronous version*: ``afirst()`` Returns the first object matched by the queryset, or ``None`` if there -is no matching object. If the ``QuerySet`` has no ordering defined, then the -queryset is automatically ordered by the primary key. This can affect +is no matching object. If the ``QuerySet`` has no ordering defined (and has not +had ordering forcibly cleared by calling :meth:`order_by` with no arguments), +then the queryset is automatically ordered by the primary key. This can affect aggregation results as described in :ref:`aggregation-ordering-interaction`. Example:: @@ -2790,6 +2791,12 @@ equivalent to the above example:: except IndexError: p = None +.. versionchanged:: 6.1 + + ``first()`` and :meth:`last` no longer order by the primary key when + ordering has been forcibly cleared by calling :meth:`order_by` with no + arguments. + ``last()`` ~~~~~~~~~~ -- cgit v1.3