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 +++++++++-- docs/releases/6.1.txt | 4 ++++ 2 files changed, 13 insertions(+), 2 deletions(-) (limited to 'docs') 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()`` ~~~~~~~~~~ diff --git a/docs/releases/6.1.txt b/docs/releases/6.1.txt index ca9ba16e84..a26b2be07f 100644 --- a/docs/releases/6.1.txt +++ b/docs/releases/6.1.txt @@ -422,6 +422,10 @@ Miscellaneous ``null``, to match the behavior of :lookup:`exact=None ` on key transforms. Previously, it was interpreted as an :lookup:`isnull` lookup. +* :meth:`~.QuerySet.first` and :meth:`~.QuerySet.last` no longer order by the + primary key when a ``QuerySet``'s ordering has been forcibly cleared by + calling :meth:`~.QuerySet.order_by` with no arguments. + .. _deprecated-features-6.1: Features deprecated in 6.1 -- cgit v1.3