Ticket #6915: 6915.naive.r7787.diff

File 6915.naive.r7787.diff, 3.7 KB (added by emulbreh, 7 years ago)
  • django/db/models/query.py

     
    351351        """
    352352        Deletes the records in the current QuerySet.
    353353        """
    354         assert self.query.can_filter(), \
    355                 "Cannot use 'limit' or 'offset' with delete."
    356354
    357355        del_query = self._clone()
    358356
    359357        # Disable non-supported fields.
    360358        del_query.query.select_related = False
    361359        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)
    362380
    363         # Delete objects in chunks to prevent the list of related objects from
    364         # becoming too long.
    365         while 1:
    366             # Collect all the objects to be deleted in this chunk, and all the
    367             # 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                 break
    374             delete_objects(seen_objs)
    375 
    376381        # Clear the result cache, in case this QuerySet gets reused.
    377382        self._result_cache = None
    378383    delete.alters_data = True
  • tests/regressiontests/queries/models.py

     
    183183class Related(models.Model):
    184184    custom = models.ForeignKey(CustomPk)
    185185
     186class Bug6915(models.Model):
     187    name = models.CharField(max_length=10)
    186188
     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
    187197__test__ = {'API_TESTS':"""
    188198>>> t1 = Tag.objects.create(name='t1')
    189199>>> t2 = Tag.objects.create(name='t2', parent=t1)
     
    637647>>> Author.objects.filter(Q(extra__note=n1)|Q(item__note=n3)).filter(id=a1.id)
    638648[<Author: a1>]
    639649
     650Bug #6915
     651Make 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
    640675Bug #6981
    641676>>> Tag.objects.select_related('parent').order_by('name')
    642677[<Tag: t1>, <Tag: t2>, <Tag: t3>, <Tag: t4>, <Tag: t5>]
Back to Top