Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #7298: prevent update() on sliced QuerySet since UPDATE doesn't…

… 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...
commit b5f92938abd64f5577eea9f80925d71bc7968c09 1 parent 1271679
Jacob Kaplan-Moss authored June 09, 2008
4  django/db/models/query.py
@@ -292,6 +292,8 @@ def update(self, **kwargs):
292 292
         Updates all elements in the current QuerySet, setting all the given
293 293
         fields to the appropriate values.
294 294
         """
  295
+        assert self.query.can_filter(), \
  296
+                "Cannot update a query once a slice has been taken."
295 297
         query = self.query.clone(sql.UpdateQuery)
296 298
         query.add_update_values(kwargs)
297 299
         query.execute_sql(None)
@@ -306,6 +308,8 @@ def _update(self, values):
306 308
         code (it requires too much poking around at model internals to be
307 309
         useful at that level).
308 310
         """
  311
+        assert self.query.can_filter(), \
  312
+                "Cannot update a query once a slice has been taken."
309 313
         query = self.query.clone(sql.UpdateQuery)
310 314
         query.add_update_fields(values)
311 315
         query.execute_sql(None)
7  tests/modeltests/update/models.py
@@ -63,5 +63,12 @@ def __unicode__(self):
63 63
 >>> DataPoint.objects.values('value').distinct()
64 64
 [{'value': u'thing'}]
65 65
 
  66
+We do not support update on already sliced query sets.
  67
+
  68
+>>> DataPoint.objects.all()[:2].update(another_value='another thing')
  69
+Traceback (most recent call last):
  70
+    ...
  71
+AssertionError: Cannot update a query once a slice has been taken.
  72
+
66 73
 """
67 74
 }

0 notes on commit b5f9293

Please sign in to comment.
Something went wrong with that request. Please try again.