On Aug 18, 11:55 pm, Chad Lyon <[email protected]> wrote: > I have a problem with the way django deletes related objects. I have > overridden the delete() method for my app so that I may do some > "specialness" when something is deleted (if you must know I log the > full deleted record to another mysql db). I have done this abstractly > for all models in my app. > > The issue is when deleting related records for a model instance django > eventually calls the following method on django.db.models.Model: > > 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) > > There is a problem with this method in that it calls > delete_objects(...) which descends into all children and deletes them > by constructing SQL (FWIU) to perform the delete. This circumvents the > delete(self, using=None) method on the Child objects. Meaning if I > have overridden delete() then my overridden routine doesn't get called > for my Children only for the Parent. This seems to be inconsistent > behavior. Don't you think delete() should be called on all seen_objs?
I think the post_delete signal is what you want. The model.delete() is meant for single object deletion. Bulk delete doesn't call it. This is documented at https://docs.djangoproject.com/en/dev/topics/db/models/#overriding-model-methods. I know that can be a little confusing, but there is a good reason for this. If you are deleting thousands of objects, you really, really don't want to delete them one at a time. -- You received this message because you are subscribed to the Google Groups "Django developers" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/django-developers?hl=en.
