Ticket #6915: 6915.naive.r7787.diff
File 6915.naive.r7787.diff, 3.7 KB (added by , 16 years ago) |
---|
-
django/db/models/query.py
351 351 """ 352 352 Deletes the records in the current QuerySet. 353 353 """ 354 assert self.query.can_filter(), \355 "Cannot use 'limit' or 'offset' with delete."356 354 357 355 del_query = self._clone() 358 356 359 357 # Disable non-supported fields. 360 358 del_query.query.select_related = False 361 359 del_query.query.clear_ordering() 360 361 # Delete objects individually if self is sliced or self.model has a custom delete() method. 362 # Use a DELETE query otherwise. 363 from django.db.models import Model 364 if not self.query.can_filter() or self.model is not None and self.model.delete != Model.delete: 365 for obj in del_query: 366 obj.delete() 367 else: 368 # Delete objects in chunks to prevent the list of related objects from 369 # becoming too long. 370 while 1: 371 # Collect all the objects to be deleted in this chunk, and all the 372 # objects that are related to the objects that are to be deleted. 373 seen_objs = CollectedObjects() 374 for object in del_query[:CHUNK_SIZE]: 375 object._collect_sub_objects(seen_objs) 376 377 if not seen_objs: 378 break 379 delete_objects(seen_objs) 362 380 363 # Delete objects in chunks to prevent the list of related objects from364 # becoming too long.365 while 1:366 # Collect all the objects to be deleted in this chunk, and all the367 # objects that are related to the objects that are to be deleted.368 seen_objs = CollectedObjects()369 for object in del_query[:CHUNK_SIZE]:370 object._collect_sub_objects(seen_objs)371 372 if not seen_objs:373 break374 delete_objects(seen_objs)375 376 381 # Clear the result cache, in case this QuerySet gets reused. 377 382 self._result_cache = None 378 383 delete.alters_data = True -
tests/regressiontests/queries/models.py
183 183 class Related(models.Model): 184 184 custom = models.ForeignKey(CustomPk) 185 185 186 class Bug6915(models.Model): 187 name = models.CharField(max_length=10) 186 188 189 deleted = [] 190 def delete(self): 191 super(Bug6915, self).delete() 192 self.__class__.deleted.append(self) 193 194 def __unicode__(self): 195 return u"%s" % self.name 196 187 197 __test__ = {'API_TESTS':""" 188 198 >>> t1 = Tag.objects.create(name='t1') 189 199 >>> t2 = Tag.objects.create(name='t2', parent=t1) … … 637 647 >>> Author.objects.filter(Q(extra__note=n1)|Q(item__note=n3)).filter(id=a1.id) 638 648 [<Author: a1>] 639 649 650 Bug #6915 651 Make shure custom Model.delete() methods are called. 652 653 >>> a = Bug6915(name="A") 654 >>> a.save() 655 >>> Bug6915.objects.all().delete() 656 >>> Bug6915.objects.all() 657 [] 658 >>> Bug6915.deleted 659 [<Bug6915: A>] 660 >>> a = Bug6915(name="A") 661 >>> a.save() 662 >>> b = Bug6915(name="B") 663 >>> b.save() 664 >>> Bug6915.objects.filter(name='B').delete() 665 >>> Bug6915.objects.all() 666 [<Bug6915: A>] 667 >>> Bug6915.deleted 668 [<Bug6915: A>, <Bug6915: B>] 669 >>> c=Bug6915(name="C") 670 >>> c.save() 671 >>> Bug6915.objects.order_by('name')[1:].delete() 672 >>> Bug6915.deleted 673 [<Bug6915: A>, <Bug6915: B>, <Bug6915: C>] 674 640 675 Bug #6981 641 676 >>> Tag.objects.select_related('parent').order_by('name') 642 677 [<Tag: t1>, <Tag: t2>, <Tag: t3>, <Tag: t4>, <Tag: t5>]