From 9f078b61ecad89eeda94ed617872271a2c029058 Mon Sep 17 00:00:00 2001
From: Sebastian Noack <sebastian.noack@gmail.com>
Date: Fri, 23 May 2008 17:11:34 +0200
Subject: [PATCH] Preventing update() on a sliced QuerySet (#7298).
---
django/db/models/query.py | 2 ++
tests/modeltests/update/models.py | 7 +++++++
2 files changed, 9 insertions(+), 0 deletions(-)
diff --git a/django/db/models/query.py b/django/db/models/query.py
index 6b341ba..9cad340 100644
a
|
b
|
class QuerySet(object):
|
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) |
diff --git a/tests/modeltests/update/models.py b/tests/modeltests/update/models.py
index 3b0f833..ab86594 100644
a
|
b
|
a manager method.
|
63 | 63 | >>> DataPoint.objects.values('value').distinct() |
64 | 64 | [{'value': u'thing'}] |
65 | 65 | |
| 66 | We can NOT update sliced query sets and that must be so. |
| 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 | } |