def delete(self): """ Deletes the records in the current QuerySet. """ assert self.query.can_filter(), \ "Cannot use 'limit' or 'offset' with delete." del_query = self._clone() # Disable non-supported fields. del_query.query.select_related = False del_query.query.clear_ordering() # Delete objects in chunks to prevent the list of related objects from # becoming too long. while 1: # Collect all the objects to be deleted in this chunk, and all the # objects that are related to the objects that are to be deleted. seen_objs = CollectedObjects() for object in del_query[:CHUNK_SIZE]: object._collect_sub_objects(seen_objs) if not seen_objs: break delete_objects(seen_objs) # Clear the result cache, in case this QuerySet gets reused. self._result_cache = None
def delete(self): """ Deletes the records in the current QuerySet. """ assert self.query.can_filter(), \ "Cannot use 'limit' or 'offset' with delete." del_query = self._clone() # Disable non-supported fields. del_query.query.select_related = False del_query.query.clear_ordering() # Delete objects in chunks to prevent the list of related objects from # becoming too long. seen_objs = None del_itr = iter(del_query) while 1: # Collect a chunk of objects to be deleted, and then all the # objects that are related to the objects that are to be deleted. # The chunking *isn't* done by slicing the del_query because we # need to maintain the query cache on del_query (see #12328) seen_objs = CollectedObjects(seen_objs) for i, obj in izip(xrange(CHUNK_SIZE), del_itr): obj._collect_sub_objects(seen_objs) if not seen_objs: break delete_objects(seen_objs) # Clear the result cache, in case this QuerySet gets reused. self._result_cache = None
def delete(self, using=None): using = using or router.db_for_write(self.__class__, instance=self) assert self._get_pk_val() is not None, "%s object can't be deleted because its %s attribute is set to None." % (self._meta.object_name, self._meta.pk.attname) # Find all the objects than need to be deleted. seen_objs = CollectedObjects() self._collect_sub_objects(seen_objs) # Actually delete the objects. delete_objects(seen_objs, using)
def delete(self): assert self._get_pk_val( ) is not None, "%s object can't be deleted because its %s attribute is set to None." % ( self._meta.object_name, self._meta.pk.attname) # Find all the objects than need to be deleted. seen_objs = CollectedObjects() self._collect_sub_objects(seen_objs) # Actually delete the objects. delete_objects(seen_objs)
def delete(self, using=None): using = using or self._state.db or DEFAULT_DB_ALIAS connection = connections[using] assert self._get_pk_val( ) is not None, "%s object can't be deleted because its %s attribute is set to None." % ( self._meta.object_name, self._meta.pk.attname) # Find all the objects than need to be deleted. seen_objs = CollectedObjects() self._collect_sub_objects(seen_objs) # Actually delete the objects. delete_objects(seen_objs, using)