Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #15118 - Corrected the deletion-ordering for inherited models.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@15246 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 621f48086e188025cff9a1a4a521439a358a8452 1 parent ca64a3c
Carl Meyer authored January 19, 2011

Showing 1 changed file with 13 additions and 5 deletions. Show diff stats Hide diff stats

  1. 18  django/db/models/deletion.py
18  django/db/models/deletion.py
@@ -64,7 +64,7 @@ def __init__(self, using):
64 64
         self.field_updates = {} # {model: {(field, value): set([instances])}}
65 65
         self.dependencies = {} # {model: set([models])}
66 66
 
67  
-    def add(self, objs, source=None, nullable=False):
  67
+    def add(self, objs, source=None, nullable=False, reverse_dependency=False):
68 68
         """
69 69
         Adds 'objs' to the collection of objects to be deleted.  If the call is
70 70
         the result of a cascade, 'source' should be the model that caused it
@@ -85,6 +85,8 @@ def add(self, objs, source=None, nullable=False):
85 85
         # deleting, and therefore do not affect the order in which objects have
86 86
         # to be deleted.
87 87
         if new_objs and source is not None and not nullable:
  88
+            if reverse_dependency:
  89
+                source, model = model, source
88 90
             self.dependencies.setdefault(source, set()).add(model)
89 91
         return new_objs
90 92
 
@@ -108,7 +110,7 @@ def add_field_update(self, field, value, objs):
108 110
             (field, value), set()).update(objs)
109 111
 
110 112
     def collect(self, objs, source=None, nullable=False, collect_related=True,
111  
-        source_attr=None):
  113
+        source_attr=None, reverse_dependency=False):
112 114
         """
113 115
         Adds 'objs' to the collection of objects to be deleted as well as all
114 116
         parent instances.  'objs' must be a homogenous iterable collection of
@@ -118,9 +120,14 @@ def collect(self, objs, source=None, nullable=False, collect_related=True,
118 120
         If the call is the result of a cascade, 'source' should be the model
119 121
         that caused it and 'nullable' should be set to True, if the relation
120 122
         can be null.
121  
-        """
122 123
 
123  
-        new_objs = self.add(objs, source, nullable)
  124
+        If 'reverse_dependency' is True, 'source' will be deleted before the
  125
+        current model, rather than after. (Needed for cascading to parent
  126
+        models, the one case in which the cascade follows the forwards
  127
+        direction of an FK rather than the reverse direction.)
  128
+        """
  129
+        new_objs = self.add(objs, source, nullable,
  130
+                            reverse_dependency=reverse_dependency)
124 131
         if not new_objs:
125 132
             return
126 133
         model = new_objs[0].__class__
@@ -132,7 +139,8 @@ def collect(self, objs, source=None, nullable=False, collect_related=True,
132 139
                 parent_objs = [getattr(obj, ptr.name) for obj in new_objs]
133 140
                 self.collect(parent_objs, source=model,
134 141
                              source_attr=ptr.rel.related_name,
135  
-                             collect_related=False)
  142
+                             collect_related=False,
  143
+                             reverse_dependency=True)
136 144
 
137 145
         if collect_related:
138 146
             for related in model._meta.get_all_related_objects(include_hidden=True):

0 notes on commit 621f480

Please sign in to comment.
Something went wrong with that request. Please try again.