Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Merge pull request #642 from dcramer/patch-1

Send post_delete signals immediately
  • Loading branch information...
commit c346d358fdca2e375a0840664165a51a47f4ae69 2 parents 4720117 + a7ed09d
Alex Gaynor authored January 14, 2013
1  AUTHORS
@@ -142,6 +142,7 @@ answer newbie questions, and generally made Django that much better:
142 142
     crankycoder@gmail.com
143 143
     Paul Collier <paul@paul-collier.com>
144 144
     Robert Coup
  145
+    David Cramer <dcramer@gmail.com>
145 146
     Pete Crosier <pete.crosier@gmail.com>
146 147
     Matt Croydon <http://www.postneo.com/>
147 148
     Jure Cuhalev <gandalf@owca.info>
15  django/db/models/deletion.py
@@ -75,17 +75,17 @@ def __init__(self, using):
75 75
         self.using = using
76 76
         # Initially, {model: set([instances])}, later values become lists.
77 77
         self.data = {}
78  
-        self.field_updates = {} # {model: {(field, value): set([instances])}}
  78
+        self.field_updates = {}  # {model: {(field, value): set([instances])}}
79 79
         # fast_deletes is a list of queryset-likes that can be deleted without
80 80
         # fetching the objects into memory.
81  
-        self.fast_deletes = [] 
  81
+        self.fast_deletes = []
82 82
 
83 83
         # Tracks deletion-order dependency for databases without transactions
84 84
         # or ability to defer constraint checks. Only concrete model classes
85 85
         # should be included, as the dependencies exist only between actual
86 86
         # database tables; proxy models are represented here by their concrete
87 87
         # parent.
88  
-        self.dependencies = {} # {model: set([models])}
  88
+        self.dependencies = {}  # {model: set([models])}
89 89
 
90 90
     def add(self, objs, source=None, nullable=False, reverse_dependency=False):
91 91
         """
@@ -301,12 +301,11 @@ def delete(self):
301 301
             pk_list = [obj.pk for obj in instances]
302 302
             query.delete_batch(pk_list, self.using)
303 303
 
304  
-        # send post_delete signals
305  
-        for model, obj in self.instances_with_model():
306 304
             if not model._meta.auto_created:
307  
-                signals.post_delete.send(
308  
-                    sender=model, instance=obj, using=self.using
309  
-                )
  305
+                for obj in instances:
  306
+                    signals.post_delete.send(
  307
+                        sender=model, instance=obj, using=self.using
  308
+                    )
310 309
 
311 310
         # update collected instances
312 311
         for model, instances_for_fieldvalues in six.iteritems(self.field_updates):
21  tests/modeltests/delete/tests.py
@@ -229,6 +229,27 @@ def log_pre_delete(sender, **kwargs):
229 229
         models.signals.post_delete.disconnect(log_post_delete)
230 230
         models.signals.post_delete.disconnect(log_pre_delete)
231 231
 
  232
+    def test_relational_post_delete_signals_happen_before_parent_object(self):
  233
+        deletions = []
  234
+
  235
+        def log_post_delete(instance, **kwargs):
  236
+            self.assertTrue(R.objects.filter(pk=instance.r_id))
  237
+            self.assertEquals(type(instance), S)
  238
+            deletions.append(instance.id)
  239
+
  240
+        r = R.objects.create(pk=1)
  241
+        S.objects.create(pk=1, r=r)
  242
+
  243
+        models.signals.post_delete.connect(log_post_delete, sender=S)
  244
+
  245
+        try:
  246
+            r.delete()
  247
+        finally:
  248
+            models.signals.post_delete.disconnect(log_post_delete)
  249
+
  250
+        self.assertEquals(len(deletions), 1)
  251
+        self.assertEquals(deletions[0], 1)
  252
+
232 253
     @skipUnlessDBFeature("can_defer_constraint_checks")
233 254
     def test_can_defer_constraint_checks(self):
234 255
         u = User.objects.create(

0 notes on commit c346d35

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