summaryrefslogtreecommitdiff
path: root/django
diff options
context:
space:
mode:
authorhb6h057 <me@hb6.org>2023-03-17 00:03:32 +0800
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2023-03-18 13:19:40 +0100
commit2ffa815c734e12262a3cb8ce5664f297128ae47f (patch)
treeac8f356d2de02b6593444d9fe568ab4b31940c04 /django
parentd2b688b966f5d30414899549412d370e1317ddb8 (diff)
downloaddjango-2ffa815c734e12262a3cb8ce5664f297128ae47f.tar.gz
Fixed #34421 -- Fixed QuerySet.update() on querysets in descending order by annotations.
Diffstat (limited to 'django')
-rw-r--r--django/db/backends/mysql/features.py1
-rw-r--r--django/db/models/query.py9
2 files changed, 9 insertions, 1 deletions
diff --git a/django/db/backends/mysql/features.py b/django/db/backends/mysql/features.py
index c657860b5d..0b8d12d2fe 100644
--- a/django/db/backends/mysql/features.py
+++ b/django/db/backends/mysql/features.py
@@ -108,6 +108,7 @@ class DatabaseFeatures(BaseDatabaseFeatures):
"update.tests.AdvancedTests."
"test_update_ordered_by_inline_m2m_annotation",
"update.tests.AdvancedTests.test_update_ordered_by_m2m_annotation",
+ "update.tests.AdvancedTests.test_update_ordered_by_m2m_annotation_desc",
},
}
if self.connection.mysql_is_mariadb and (
diff --git a/django/db/models/query.py b/django/db/models/query.py
index 6ac2a4cb10..56ad4d5c20 100644
--- a/django/db/models/query.py
+++ b/django/db/models/query.py
@@ -1190,11 +1190,18 @@ class QuerySet(AltersData):
# Inline annotations in order_by(), if possible.
new_order_by = []
for col in query.order_by:
- if annotation := query.annotations.get(col):
+ alias = col
+ descending = False
+ if isinstance(alias, str) and alias.startswith("-"):
+ alias = alias.removeprefix("-")
+ descending = True
+ if annotation := query.annotations.get(alias):
if getattr(annotation, "contains_aggregate", False):
raise exceptions.FieldError(
f"Cannot update when ordering by an aggregate: {annotation}"
)
+ if descending:
+ annotation = annotation.desc()
new_order_by.append(annotation)
else:
new_order_by.append(col)