def handle_noargs(self, **options): from django.db.models import get_apps, get_models print " CHECKING CASCADE DELETE" for app in get_apps(): loaded_models = get_models(app) print "%30s" % app.__name__.upper().replace('.MODELS','') for x in loaded_models: model_name = unicode(x._meta.verbose_name).title() print "%30s :" % model_name, seen_count = 0 seen_types = [] try: while 1: objs = x.objects.all().order_by('?')[:LOOP] seen_objs = CollectedObjects() for obj in objs: obj._collect_sub_objects(seen_objs, nullable=False) seen_types = [] for seen_cls, seen_objs in seen_objs.items(): if seen_cls not in seen_types: seen_types.append(seen_cls) if len(seen_types)==seen_count: break seen_count = len(seen_types) print [ x.__name__ for x in seen_types ] except Exception, e: print unicode(e) print "" print ""
def get_related_objects(obj): ''' Return all related objects to this object. These objects will be deleted when the object is deleted. Return form is [(ModelClass, {id, instance}),] ''' seen_objects = CollectedObjects() obj._collect_sub_objects(seen_objects) return seen_objects.items()
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_marked_for_deletion(self, collect=True): """If this instance, or some remote instances are marked for deletion kill them. """ if collect: from django.db.models.query_utils import CollectedObjects seen = CollectedObjects() self._collect_sub_objects(seen) for cls, items in seen.items(): if issubclass(cls, PublicPublisher): for item in items.values(): item.delete_marked_for_deletion(collect=False) if self.mark_delete: self.delete()
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 _collect_deleted_objects(self, obj): result = [] try: # This is for Django up to 1.2 from django.db.models.query_utils import CollectedObjects seen_objs = CollectedObjects() obj._collect_sub_objects(seen_objs) for cls, subobjs in seen_objs.iteritems(): for subobj in subobjs.values(): result.append(subobj) except ImportError: # Django 1.3 solution, those imports needs to be here, because # otherwise they will fail on Django < 1.3. from django.contrib.admin.util import NestedObjects from django.db import router using = router.db_for_write(obj) collector = NestedObjects(using=using) collector.collect([obj]) result = self._flatten(collector.nested()) return result
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)
def _publisher_delete_marked(self, collect=True): """If this instance, or some remote instances are marked for deletion kill them. """ if self.publisher_is_draft: # escape soon from draft models return if collect: from django.db.models.query_utils import CollectedObjects seen = CollectedObjects() self._collect_sub_objects(seen) for cls, items in seen.items(): if issubclass(cls, Publisher): for item in items.values(): item._publisher_delete_marked(collect=False) if self.publisher_state == Publisher.PUBLISHER_STATE_DELETE: try: self.delete() except AttributeError: # this exception may happen because of the plugin relations # to CMSPlugin and mppt way of _meta assignment pass