Ticket #7298: 0001-Preventing-update-on-a-sliced-QuerySet-7298.patch

File 0001-Preventing-update-on-a-sliced-QuerySet-7298.patch, 1.5 KB (added by sebastian_noack, 7 years ago)
  • django/db/models/query.py

    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): 
    292292        Updates all elements in the current QuerySet, setting all the given
    293293        fields to the appropriate values.
    294294        """
     295        assert self.query.can_filter(), \
     296                "Cannot update a query once a slice has been taken."
    295297        query = self.query.clone(sql.UpdateQuery)
    296298        query.add_update_values(kwargs)
    297299        query.execute_sql(None)
  • tests/modeltests/update/models.py

    diff --git a/tests/modeltests/update/models.py b/tests/modeltests/update/models.py
    index 3b0f833..ab86594 100644
    a b a manager method. 
    6363>>> DataPoint.objects.values('value').distinct()
    6464[{'value': u'thing'}]
    6565
     66We can NOT update sliced query sets and that must be so.
     67
     68>>> DataPoint.objects.all()[:2].update(another_value='another thing')
     69Traceback (most recent call last):
     70    ...
     71AssertionError: Cannot update a query once a slice has been taken.
     72
    6673"""
    6774}
Back to Top