Skip to content

Commit

Permalink
Fixed #7298: prevent update() on sliced QuerySet since UPDATE doesn't…
Browse files Browse the repository at this point in the history
… reliably support LIMIT/OFFSET. Thanks, George Vilches.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@7601 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information
jacobian committed Jun 9, 2008
1 parent 1271679 commit b5f9293
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
4 changes: 4 additions & 0 deletions django/db/models/query.py
Expand Up @@ -292,6 +292,8 @@ def update(self, **kwargs):
Updates all elements in the current QuerySet, setting all the given Updates all elements in the current QuerySet, setting all the given
fields to the appropriate values. fields to the appropriate values.
""" """
assert self.query.can_filter(), \
"Cannot update a query once a slice has been taken."
query = self.query.clone(sql.UpdateQuery) query = self.query.clone(sql.UpdateQuery)
query.add_update_values(kwargs) query.add_update_values(kwargs)
query.execute_sql(None) query.execute_sql(None)
Expand All @@ -306,6 +308,8 @@ def _update(self, values):
code (it requires too much poking around at model internals to be code (it requires too much poking around at model internals to be
useful at that level). useful at that level).
""" """
assert self.query.can_filter(), \
"Cannot update a query once a slice has been taken."
query = self.query.clone(sql.UpdateQuery) query = self.query.clone(sql.UpdateQuery)
query.add_update_fields(values) query.add_update_fields(values)
query.execute_sql(None) query.execute_sql(None)
Expand Down
7 changes: 7 additions & 0 deletions tests/modeltests/update/models.py
Expand Up @@ -63,5 +63,12 @@ def __unicode__(self):
>>> DataPoint.objects.values('value').distinct() >>> DataPoint.objects.values('value').distinct()
[{'value': u'thing'}] [{'value': u'thing'}]
We do not support update on already sliced query sets.
>>> DataPoint.objects.all()[:2].update(another_value='another thing')
Traceback (most recent call last):
...
AssertionError: Cannot update a query once a slice has been taken.
""" """
} }

0 comments on commit b5f9293

Please sign in to comment.