Ticket #12328: bug12328.patch
File bug12328.patch, 3.9 KB (added by , 15 years ago) |
---|
-
django/db/models/sql/compiler.py
130 130 Used when nesting this query inside another. 131 131 """ 132 132 obj = self.query.clone() 133 obj.clear_ordering(True) 133 if obj.low_mark == 0 and obj.high_mark is None: 134 obj.clear_ordering(True) 134 135 obj.bump_prefix() 135 136 return obj.get_compiler(connection=self.connection).as_sql() 136 137 -
django/db/models/query.py
10 10 from django.db.models.query_utils import Q, select_related_descend, CollectedObjects, CyclicDependency, deferred_class_factory, InvalidQuery 11 11 from django.db.models import signals, sql 12 12 from django.utils.copycompat import deepcopy 13 from django.db.sql.where import WhereNode 13 14 14 15 # Used to control how many objects are worked with at once in some cases (e.g. 15 16 # when deleting objects). … … 407 408 qs.query.add_filter(('pk__in', id_list)) 408 409 return dict([(obj._get_pk_val(), obj) for obj in qs.iterator()]) 409 410 411 def _verify_before_delete(self, children = []): 412 for child in children: 413 if isinstance(child, tuple): 414 if isinstance(child[-1], QuerySet): 415 assert child[-1].query.can_filter(), \ 416 "Cannot use 'limit' or 'offset' with delete." 417 elif isinstance(child, WhereNode): 418 self._verify_before_delete(child.children) 419 410 420 def delete(self): 411 421 """ 412 422 Deletes the records in the current QuerySet. … … 414 424 assert self.query.can_filter(), \ 415 425 "Cannot use 'limit' or 'offset' with delete." 416 426 427 self._verify_before_delete(self.query.where.children) 428 417 429 del_query = self._clone() 418 430 419 431 # The delete is actually 2 queries - one to find related objects, -
tests/regressiontests/bug12328/tests.py
1 from unittest import TestCase 2 from models import Bug12328 3 4 class TestBug12328(TestCase): 5 def test_delete(self): 6 Bug12328.objects.create(id=1) 7 Bug12328.objects.create(id=2) 8 Bug12328.objects.create(id=3) 9 expected = Bug12328.objects.filter(id__in=[2,3]) 10 Bug12328.objects.filter(id=1).delete() 11 self.assertEquals(expected.__repr__(), Bug12328.objects.all().__repr__()) 12 Bug12328.objects.create(id=1) 13 exp_element = Bug12328.objects.order_by('id')[0:1] 14 query = Bug12328.objects.filter(id__in=Bug12328.objects.order_by('id')[0:1].values_list('id', flat=True)) 15 query2 = Bug12328.objects.filter(id__in=Bug12328.objects.order_by('id')[0:1]) 16 self.assertEquals(exp_element.__repr__(), query.__repr__(), query2.__repr__()) 17 # Catch the exception 18 try: 19 Bug12328.objects.filter(id__in=Bug12328.objects.order_by('id')[0:1]).delete() 20 except AssertionError, e: 21 self.assertEquals(str(e), "Cannot use 'limit' or 'offset' with delete.") -
tests/regressiontests/bug12328/models.py
1 from django.db import models 2 3 # Create your models here. 4 5 class Bug12328(models.Model): 6 def __unicode__(self): 7 return 'M(id=%d)' % self.id