From 65ad4ade74dc9208b9d686a451cd6045df0c9c3a Mon Sep 17 00:00:00 2001 From: Simon Charette Date: Tue, 28 Mar 2023 00:13:00 -0400 Subject: Refs #28900 -- Made SELECT respect the order specified by values(*selected). Previously the order was always extra_fields + model_fields + annotations with respective local ordering inferred from the insertion order of *selected. This commits introduces a new `Query.selected` propery that keeps tracks of the global select order as specified by on values assignment. This is crucial feature to allow the combination of queries mixing annotations and table references. It also allows the removal of the re-ordering shenanigans perform by ValuesListIterable in order to re-map the tuples returned from the database backend to the order specified by values_list() as they'll be in the right order at query compilation time. Refs #28553 as the initially reported issue that was only partially fixed for annotations by d6b6e5d0fd4e6b6d0183b4cf6e4bd4f9afc7bf67. Thanks Mariusz Felisiak and Sarah Boyce for review. --- docs/ref/models/querysets.txt | 10 ++++++++++ docs/releases/5.2.txt | 8 +++++++- docs/spelling_wordlist | 1 + 3 files changed, 18 insertions(+), 1 deletion(-) (limited to 'docs') diff --git a/docs/ref/models/querysets.txt b/docs/ref/models/querysets.txt index 7a0d086bfe..d708e05a79 100644 --- a/docs/ref/models/querysets.txt +++ b/docs/ref/models/querysets.txt @@ -745,6 +745,11 @@ You can also refer to fields on related models with reverse relations through ``"true"``, ``"false"``, and ``"null"`` strings for :class:`~django.db.models.JSONField` key transforms. +.. versionchanged:: 5.2 + + The ``SELECT`` clause generated when using ``values()`` was updated to + respect the order of the specified ``*fields`` and ``**expressions``. + ``values_list()`` ~~~~~~~~~~~~~~~~~ @@ -835,6 +840,11 @@ not having any author: ``"true"``, ``"false"``, and ``"null"`` strings for :class:`~django.db.models.JSONField` key transforms. +.. versionchanged:: 5.2 + + The ``SELECT`` clause generated when using ``values_list()`` was updated to + respect the order of the specified ``*fields``. + ``dates()`` ~~~~~~~~~~~ diff --git a/docs/releases/5.2.txt b/docs/releases/5.2.txt index bdc5349368..5d5887fe34 100644 --- a/docs/releases/5.2.txt +++ b/docs/releases/5.2.txt @@ -195,7 +195,13 @@ Migrations Models ~~~~~~ -* ... +* The ``SELECT`` clause generated when using + :meth:`QuerySet.values()` and + :meth:`~django.db.models.query.QuerySet.values_list` now matches the + specified order of the referenced expressions. Previously the order was based + of a set of counterintuitive rules which made query combination through + methods such as + :meth:`QuerySet.union()` unpredictable. Requests and Responses ~~~~~~~~~~~~~~~~~~~~~~ diff --git a/docs/spelling_wordlist b/docs/spelling_wordlist index 1044cd80eb..d715e62e05 100644 --- a/docs/spelling_wordlist +++ b/docs/spelling_wordlist @@ -96,6 +96,7 @@ contenttypes contrib coroutine coroutines +counterintuitive criticals cron crontab -- cgit v1.3