Permalink
Browse files

Remove annotations from model updates after resolving update values

  • Loading branch information...
dsanders11 committed Apr 17, 2016
1 parent 9686c88 commit 4431905249ffb8684127ea809688c80f4a64f051
Showing with 13 additions and 1 deletion.
  1. +6 −0 django/db/models/query.py
  2. +7 −1 django/db/models/sql/subqueries.py
@@ -635,6 +635,9 @@ def update(self, **kwargs):
self._for_write = True
query = self.query.clone(sql.UpdateQuery)
query.add_update_values(kwargs)
# Clear any annotations so that they won't be present in subqueries
query.set_annotation_mask(None)
query._annotations = None
with transaction.atomic(using=self.db, savepoint=False):
rows = query.get_compiler(self.db).execute_sql(CURSOR)
self._result_cache = None
@@ -652,6 +655,9 @@ def _update(self, values):
"Cannot update a query once a slice has been taken."
query = self.query.clone(sql.UpdateQuery)
query.add_update_fields(values)
# Clear any annotations so that they won't be present in subqueries
query.set_annotation_mask(None)
query._annotations = None
self._result_cache = None
return query.get_compiler(self.db).execute_sql(CURSOR)
_update.alters_data = True
@@ -142,7 +142,13 @@ def add_update_fields(self, values_seq):
that will be used to generate the UPDATE query. Might be more usefully
called add_update_targets() to hint at the extra information here.
"""
self.values.extend(values_seq)
for field, model, val in values_seq:
if hasattr(val, 'resolve_expression'):
# Resolve expressions here so that annotations are no longer needed
val = val.resolve_expression(self, allow_joins=False, for_save=True)
if val.contains_aggregate:
raise FieldError("Aggregate functions are not allowed in this query")
self.values.append((field, model, val))
def add_related_update(self, model, field, value):
"""

0 comments on commit 4431905

Please sign in to comment.