Ticket #6915: 6915.naive-patch.2.diff

File 6915.naive-patch.2.diff, 2.2 KB (added by Johannes Dollinger, 16 years ago)
  • django/db/models/query.py

     
    261261        """
    262262        Deletes the records in the current QuerySet.
    263263        """
    264         assert self.query.can_filter(), \
    265                 "Cannot use 'limit' or 'offset' with delete."
    266 
    267264        del_query = self._clone()
    268265
    269266        # Disable non-supported fields.
    270267        del_query.query.select_related = False
    271268        del_query.query.clear_ordering()
    272269
    273         # Delete objects in chunks to prevent the list of related objects from
    274         # becoming too long.
    275         while 1:
    276             # Collect all the objects to be deleted in this chunk, and all the
    277             # objects that are related to the objects that are to be deleted.
    278             seen_objs = SortedDict()
    279             for object in del_query[:CHUNK_SIZE]:
    280                 object._collect_sub_objects(seen_objs)
    281 
    282             if not seen_objs:
    283                 break
    284             delete_objects(seen_objs)
    285 
     270        # Delete objects individually if self is sliced or self.model has a custom delete() method.
     271        # Use a DELETE query otherwise.
     272        # See ticket #6915.
     273        from django.db.models import Model
     274        if not self.query.can_filter() or self.model is not None and self.model.delete != Model.delete:
     275            for obj in del_query:
     276                obj.delete()
     277               
     278        else:
     279            # Delete objects in chunks to prevent the list of related objects from
     280            # becoming too long.
     281            while 1:
     282                # Collect all the objects to be deleted in this chunk, and all the
     283                # objects that are related to the objects that are to be deleted.
     284                seen_objs = SortedDict()
     285                for object in del_query[:CHUNK_SIZE]:
     286                    object._collect_sub_objects(seen_objs)
     287   
     288                if not seen_objs:
     289                    break
     290                delete_objects(seen_objs)
     291   
    286292        # Clear the result cache, in case this QuerySet gets reused.
    287293        self._result_cache = None
    288294    delete.alters_data = True
Back to Top