Author: carljm
Date: 2011-01-19 14:11:56 -0600 (Wed, 19 Jan 2011)
New Revision: 15246

Modified:
   django/trunk/django/db/models/deletion.py
Log:
Fixed #15118 - Corrected the deletion-ordering for inherited models.

Modified: django/trunk/django/db/models/deletion.py
===================================================================
--- django/trunk/django/db/models/deletion.py   2011-01-19 16:36:20 UTC (rev 
15245)
+++ django/trunk/django/db/models/deletion.py   2011-01-19 20:11:56 UTC (rev 
15246)
@@ -64,7 +64,7 @@
         self.field_updates = {} # {model: {(field, value): set([instances])}}
         self.dependencies = {} # {model: set([models])}
 
-    def add(self, objs, source=None, nullable=False):
+    def add(self, objs, source=None, nullable=False, reverse_dependency=False):
         """
         Adds 'objs' to the collection of objects to be deleted.  If the call is
         the result of a cascade, 'source' should be the model that caused it
@@ -85,6 +85,8 @@
         # deleting, and therefore do not affect the order in which objects have
         # to be deleted.
         if new_objs and source is not None and not nullable:
+            if reverse_dependency:
+                source, model = model, source
             self.dependencies.setdefault(source, set()).add(model)
         return new_objs
 
@@ -108,7 +110,7 @@
             (field, value), set()).update(objs)
 
     def collect(self, objs, source=None, nullable=False, collect_related=True,
-        source_attr=None):
+        source_attr=None, reverse_dependency=False):
         """
         Adds 'objs' to the collection of objects to be deleted as well as all
         parent instances.  'objs' must be a homogenous iterable collection of
@@ -118,9 +120,14 @@
         If the call is the result of a cascade, 'source' should be the model
         that caused it and 'nullable' should be set to True, if the relation
         can be null.
+
+        If 'reverse_dependency' is True, 'source' will be deleted before the
+        current model, rather than after. (Needed for cascading to parent
+        models, the one case in which the cascade follows the forwards
+        direction of an FK rather than the reverse direction.)
         """
-
-        new_objs = self.add(objs, source, nullable)
+        new_objs = self.add(objs, source, nullable,
+                            reverse_dependency=reverse_dependency)
         if not new_objs:
             return
         model = new_objs[0].__class__
@@ -132,7 +139,8 @@
                 parent_objs = [getattr(obj, ptr.name) for obj in new_objs]
                 self.collect(parent_objs, source=model,
                              source_attr=ptr.rel.related_name,
-                             collect_related=False)
+                             collect_related=False,
+                             reverse_dependency=True)
 
         if collect_related:
             for related in 
model._meta.get_all_related_objects(include_hidden=True):

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to django-updates@googlegroups.com.
To unsubscribe from this group, send email to 
django-updates+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en.

Reply via email to