1
0
mirror of https://github.com/django/django.git synced 2025-10-29 16:46:11 +00:00

Fixed #28897 -- Fixed QuerySet.update() on querysets ordered by annotations.

This commit is contained in:
David Wobrock
2022-06-17 09:20:27 +02:00
committed by Mariusz Felisiak
parent f4680a112d
commit 3ef37a5245
3 changed files with 38 additions and 8 deletions

View File

@@ -1169,6 +1169,20 @@ class QuerySet:
self._for_write = True
query = self.query.chain(sql.UpdateQuery)
query.add_update_values(kwargs)
# Inline annotations in order_by(), if possible.
new_order_by = []
for col in query.order_by:
if annotation := query.annotations.get(col):
if getattr(annotation, "contains_aggregate", False):
raise exceptions.FieldError(
f"Cannot update when ordering by an aggregate: {annotation}"
)
new_order_by.append(annotation)
else:
new_order_by.append(col)
query.order_by = tuple(new_order_by)
# Clear any annotations so that they won't be present in subqueries.
query.annotations = {}
with transaction.mark_for_rollback_on_error(using=self.db):