From b5f92938abd64f5577eea9f80925d71bc7968c09 Mon Sep 17 00:00:00 2001 From: Jacob Kaplan-Moss Date: Mon, 9 Jun 2008 16:17:54 +0000 Subject: [PATCH] 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 --- django/db/models/query.py | 4 ++++ tests/modeltests/update/models.py | 7 +++++++ 2 files changed, 11 insertions(+) diff --git a/django/db/models/query.py b/django/db/models/query.py index 6b341ba9ab..12731caa94 100644 --- a/django/db/models/query.py +++ b/django/db/models/query.py @@ -292,6 +292,8 @@ class QuerySet(object): Updates all elements in the current QuerySet, setting all the given 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.add_update_values(kwargs) query.execute_sql(None) @@ -306,6 +308,8 @@ class QuerySet(object): code (it requires too much poking around at model internals to be 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.add_update_fields(values) query.execute_sql(None) diff --git a/tests/modeltests/update/models.py b/tests/modeltests/update/models.py index 3b0f83389f..8a35b61a7c 100644 --- a/tests/modeltests/update/models.py +++ b/tests/modeltests/update/models.py @@ -63,5 +63,12 @@ a manager method. >>> DataPoint.objects.values('value').distinct() [{'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. + """ }