Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #4879 -- Added 'created' arg to post_save signal. This is True …

…is a new object is created. Patch from George Vilches.

Fully backwards compatible, because signal receivers do not have to be able to accept all the arguments (thankyou, robust_apply()).


git-svn-id: http://code.djangoproject.com/svn/django/trunk@6269 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 1baae32e16e8eee5bc239bfea8674651b765a41f 1 parent 84e824f
Malcolm Tredinnick authored September 15, 2007
1  AUTHORS
@@ -296,6 +296,7 @@ answer newbie questions, and generally made Django that much better:
296 296
     Amit Upadhyay
297 297
     Geert Vanderkelen
298 298
     viestards.lists@gmail.com
  299
+    George Vilches <gav@thataddress.com>
299 300
     Vlado <vlado@labath.org>
300 301
     Milton Waddams
301 302
     wam-djangobug@wamber.net
4  django/db/models/base.py
@@ -247,6 +247,7 @@ def save(self, raw=False):
247 247
                     qn(self._meta.order_with_respect_to.column))
248 248
                 cursor.execute(subsel, (getattr(self, self._meta.order_with_respect_to.attname),))
249 249
                 db_values.append(cursor.fetchone()[0])
  250
+            record_exists = False
250 251
             if db_values:
251 252
                 cursor.execute("INSERT INTO %s (%s) VALUES (%s)" % \
252 253
                     (qn(self._meta.db_table), ','.join(field_names),
@@ -261,7 +262,8 @@ def save(self, raw=False):
261 262
         transaction.commit_unless_managed()
262 263
 
263 264
         # Run any post-save hooks.
264  
-        dispatcher.send(signal=signals.post_save, sender=self.__class__, instance=self)
  265
+        dispatcher.send(signal=signals.post_save, sender=self.__class__,
  266
+                instance=self, created=(not record_exists))
265 267
 
266 268
     save.alters_data = True
267 269
 
0  tests/modeltests/signals/__init__.py
No changes.
85  tests/modeltests/signals/models.py
... ...
@@ -0,0 +1,85 @@
  1
+"""
  2
+Testing signals before/after saving and deleting.
  3
+"""
  4
+
  5
+from django.db import models
  6
+from django.dispatch import dispatcher
  7
+
  8
+class Person(models.Model):
  9
+    first_name = models.CharField(max_length=20)
  10
+    last_name = models.CharField(max_length=20)
  11
+
  12
+    def __unicode__(self):
  13
+        return u"%s %s" % (self.first_name, self.last_name)
  14
+
  15
+
  16
+def pre_save_test(sender, instance, **kwargs):
  17
+    print 'pre_save signal,', instance
  18
+
  19
+def post_save_test(sender, instance, **kwargs):
  20
+    print 'post_save signal,', instance
  21
+    if 'created' in kwargs:
  22
+        if kwargs['created']:
  23
+            print 'Is created'
  24
+        else:
  25
+            print 'Is updated'
  26
+
  27
+def pre_delete_test(sender, instance, **kwargs):
  28
+    print 'pre_delete signal,', instance
  29
+    print 'instance.id is not None: %s' % (instance.id != None)  
  30
+
  31
+def post_delete_test(sender, instance, **kwargs):
  32
+    print 'post_delete signal,', instance
  33
+    print 'instance.id is None: %s' % (instance.id == None)  
  34
+
  35
+__test__ = {'API_TESTS':"""
  36
+>>> dispatcher.connect(pre_save_test, signal=models.signals.pre_save)
  37
+>>> dispatcher.connect(post_save_test, signal=models.signals.post_save)
  38
+>>> dispatcher.connect(pre_delete_test, signal=models.signals.pre_delete)
  39
+>>> dispatcher.connect(post_delete_test, signal=models.signals.post_delete)
  40
+
  41
+>>> p1 = Person(first_name='John', last_name='Smith')
  42
+>>> p1.save()
  43
+pre_save signal, John Smith
  44
+post_save signal, John Smith
  45
+Is created
  46
+
  47
+>>> p1.first_name = 'Tom'
  48
+>>> p1.save()
  49
+pre_save signal, Tom Smith
  50
+post_save signal, Tom Smith
  51
+Is updated
  52
+
  53
+>>> p1.delete()
  54
+pre_delete signal, Tom Smith
  55
+instance.id is not None: True
  56
+post_delete signal, Tom Smith
  57
+instance.id is None: True
  58
+
  59
+>>> p2 = Person(first_name='James', last_name='Jones')
  60
+>>> p2.id = 99999
  61
+>>> p2.save()
  62
+pre_save signal, James Jones
  63
+post_save signal, James Jones
  64
+Is created
  65
+
  66
+>>> p2.id = 99998
  67
+>>> p2.save()
  68
+pre_save signal, James Jones
  69
+post_save signal, James Jones
  70
+Is created
  71
+
  72
+>>> p2.delete()
  73
+pre_delete signal, James Jones
  74
+instance.id is not None: True
  75
+post_delete signal, James Jones
  76
+instance.id is None: True
  77
+
  78
+>>> Person.objects.all()
  79
+[<Person: James Jones>]
  80
+
  81
+>>> dispatcher.disconnect(post_delete_test, signal=models.signals.post_delete)
  82
+>>> dispatcher.disconnect(pre_delete_test, signal=models.signals.pre_delete)
  83
+>>> dispatcher.disconnect(post_save_test, signal=models.signals.post_save)
  84
+>>> dispatcher.disconnect(pre_save_test, signal=models.signals.pre_save)
  85
+"""}

0 notes on commit 1baae32

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