Django

Code

Show
Ignore:
Timestamp:
06/29/08 04:41:35 (6 months ago)
Author:
mtredinnick
Message:

Fixed #7276 -- Delete multi-table objects correctly.

When model inheritance is used, the parent objects should be deleted as part of
the delete() call on the child.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/db/models/base.py

    r7777 r7784  
    396396                    sub_obj._collect_sub_objects(seen_objs, self.__class__, related.field.null) 
    397397 
     398        # Handle any ancestors (for the model-inheritance case). We do this by 
     399        # traversing to the most remote parent classes -- those with no parents 
     400        # themselves -- and then adding those instances to the collection. That 
     401        # will include all the child instances down to "self". 
     402        parent_stack = self._meta.parents.values() 
     403        while parent_stack: 
     404            link = parent_stack.pop() 
     405            parent_obj = getattr(self, link.name) 
     406            if parent_obj._meta.parents: 
     407                parent_stack.extend(parent_obj._meta.parents.values()) 
     408                continue 
     409            # At this point, parent_obj is base class (no ancestor models). So 
     410            # delete it and all its descendents. 
     411            parent_obj._collect_sub_objects(seen_objs) 
     412 
    398413    def delete(self): 
    399414        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)