Ticket #7298: prevent_update_queryset_r7599.patch

File prevent_update_queryset_r7599.patch, 1.5 KB (added by gav, 7 years ago)

Prevents updates on querysets, patched against r7599

  • django/db/models/query.py

     
    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)
     
    306308        code (it requires too much poking around at model internals to be
    307309        useful at that level).
    308310        """
     311        assert self.query.can_filter(), \
     312                "Cannot update a query once a slice has been taken."
    309313        query = self.query.clone(sql.UpdateQuery)
    310314        query.add_update_fields(values)
    311315        query.execute_sql(None)
  • tests/modeltests/update/models.py

     
    6363>>> DataPoint.objects.values('value').distinct()
    6464[{'value': u'thing'}]
    6565
     66We do not support update on already sliced query sets.
     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