Fixed #5422 -- Added a `raw` parameter to model `pre_save` and `post_save` signals, based on patch from `graham.carlyle@maplecroft.net`.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7054 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
0e81ad4e66
commit
cc7c6f3e46
|
@ -209,7 +209,8 @@ class Model(object):
|
|||
_prepare = classmethod(_prepare)
|
||||
|
||||
def save(self, raw=False):
|
||||
dispatcher.send(signal=signals.pre_save, sender=self.__class__, instance=self)
|
||||
dispatcher.send(signal=signals.pre_save, sender=self.__class__,
|
||||
instance=self, raw=raw)
|
||||
|
||||
non_pks = [f for f in self._meta.fields if not f.primary_key]
|
||||
cursor = connection.cursor()
|
||||
|
@ -270,7 +271,7 @@ class Model(object):
|
|||
|
||||
# Run any post-save hooks.
|
||||
dispatcher.send(signal=signals.post_save, sender=self.__class__,
|
||||
instance=self, created=(not record_exists))
|
||||
instance=self, created=(not record_exists), raw=raw)
|
||||
|
||||
save.alters_data = True
|
||||
|
||||
|
|
|
@ -13,8 +13,16 @@ class Person(models.Model):
|
|||
return u"%s %s" % (self.first_name, self.last_name)
|
||||
|
||||
|
||||
def pre_save_nokwargs_test(sender, instance):
|
||||
print 'pre_save_nokwargs signal'
|
||||
|
||||
def post_save_nokwargs_test(sender, instance):
|
||||
print 'post_save_nokwargs signal'
|
||||
|
||||
def pre_save_test(sender, instance, **kwargs):
|
||||
print 'pre_save signal,', instance
|
||||
if kwargs.get('raw'):
|
||||
print 'Is raw'
|
||||
|
||||
def post_save_test(sender, instance, **kwargs):
|
||||
print 'post_save signal,', instance
|
||||
|
@ -23,16 +31,20 @@ def post_save_test(sender, instance, **kwargs):
|
|||
print 'Is created'
|
||||
else:
|
||||
print 'Is updated'
|
||||
if kwargs.get('raw'):
|
||||
print 'Is raw'
|
||||
|
||||
def pre_delete_test(sender, instance, **kwargs):
|
||||
print 'pre_delete signal,', instance
|
||||
print 'instance.id is not None: %s' % (instance.id != None)
|
||||
print 'instance.id is not None: %s' % (instance.id != None)
|
||||
|
||||
def post_delete_test(sender, instance, **kwargs):
|
||||
print 'post_delete signal,', instance
|
||||
print 'instance.id is None: %s' % (instance.id == None)
|
||||
print 'instance.id is None: %s' % (instance.id == None)
|
||||
|
||||
__test__ = {'API_TESTS':"""
|
||||
>>> dispatcher.connect(pre_save_nokwargs_test, signal=models.signals.pre_save)
|
||||
>>> dispatcher.connect(post_save_nokwargs_test, signal=models.signals.post_save)
|
||||
>>> dispatcher.connect(pre_save_test, signal=models.signals.pre_save)
|
||||
>>> dispatcher.connect(post_save_test, signal=models.signals.post_save)
|
||||
>>> dispatcher.connect(pre_delete_test, signal=models.signals.pre_delete)
|
||||
|
@ -40,16 +52,29 @@ __test__ = {'API_TESTS':"""
|
|||
|
||||
>>> p1 = Person(first_name='John', last_name='Smith')
|
||||
>>> p1.save()
|
||||
pre_save_nokwargs signal
|
||||
pre_save signal, John Smith
|
||||
post_save_nokwargs signal
|
||||
post_save signal, John Smith
|
||||
Is created
|
||||
|
||||
>>> p1.first_name = 'Tom'
|
||||
>>> p1.save()
|
||||
pre_save_nokwargs signal
|
||||
pre_save signal, Tom Smith
|
||||
post_save_nokwargs signal
|
||||
post_save signal, Tom Smith
|
||||
Is updated
|
||||
|
||||
>>> p1.save(raw=True)
|
||||
pre_save_nokwargs signal
|
||||
pre_save signal, Tom Smith
|
||||
Is raw
|
||||
post_save_nokwargs signal
|
||||
post_save signal, Tom Smith
|
||||
Is updated
|
||||
Is raw
|
||||
|
||||
>>> p1.delete()
|
||||
pre_delete signal, Tom Smith
|
||||
instance.id is not None: True
|
||||
|
@ -59,13 +84,17 @@ instance.id is None: False
|
|||
>>> p2 = Person(first_name='James', last_name='Jones')
|
||||
>>> p2.id = 99999
|
||||
>>> p2.save()
|
||||
pre_save_nokwargs signal
|
||||
pre_save signal, James Jones
|
||||
post_save_nokwargs signal
|
||||
post_save signal, James Jones
|
||||
Is created
|
||||
|
||||
>>> p2.id = 99998
|
||||
>>> p2.save()
|
||||
pre_save_nokwargs signal
|
||||
pre_save signal, James Jones
|
||||
post_save_nokwargs signal
|
||||
post_save signal, James Jones
|
||||
Is created
|
||||
|
||||
|
@ -78,6 +107,8 @@ instance.id is None: False
|
|||
>>> Person.objects.all()
|
||||
[<Person: James Jones>]
|
||||
|
||||
>>> dispatcher.disconnect(pre_save_nokwargs_test, signal=models.signals.pre_save)
|
||||
>>> dispatcher.disconnect(post_save_nokwargs_test, signal=models.signals.post_save)
|
||||
>>> dispatcher.disconnect(post_delete_test, signal=models.signals.post_delete)
|
||||
>>> dispatcher.disconnect(pre_delete_test, signal=models.signals.pre_delete)
|
||||
>>> dispatcher.disconnect(post_save_test, signal=models.signals.post_save)
|
||||
|
|
Loading…
Reference in New Issue