mirror of
https://github.com/django/django.git
synced 2024-12-25 02:26:12 +00:00
34a3bd5225
Though some attempts and backwards-compatibility were made, speed trumped compatibility. Thus, as usual, check BackwardsIncompatibleChanges for the complete list of backwards-incompatible changes. Thanks to Jeremy Dunck and Keith Busell for the bulk of the work; some ideas from Brian Herring's previous work (refs #4561) were incorporated. Documentation is, sigh, still forthcoming. Fixes #6814 and #3951 (with the new dispatch_uid argument to connect). git-svn-id: http://code.djangoproject.com/svn/django/trunk@8223 bcc190cf-cafb-0310-a4f2-bffc1f526a37
96 lines
2.5 KiB
Python
96 lines
2.5 KiB
Python
"""
|
|
Testing signals before/after saving and deleting.
|
|
"""
|
|
|
|
from django.db import models
|
|
|
|
class Person(models.Model):
|
|
first_name = models.CharField(max_length=20)
|
|
last_name = models.CharField(max_length=20)
|
|
|
|
def __unicode__(self):
|
|
return u"%s %s" % (self.first_name, self.last_name)
|
|
|
|
def pre_save_test(signal, sender, instance, **kwargs):
|
|
print 'pre_save signal,', instance
|
|
if kwargs.get('raw'):
|
|
print 'Is raw'
|
|
|
|
def post_save_test(signal, sender, instance, **kwargs):
|
|
print 'post_save signal,', instance
|
|
if 'created' in kwargs:
|
|
if kwargs['created']:
|
|
print 'Is created'
|
|
else:
|
|
print 'Is updated'
|
|
if kwargs.get('raw'):
|
|
print 'Is raw'
|
|
|
|
def pre_delete_test(signal, sender, instance, **kwargs):
|
|
print 'pre_delete signal,', instance
|
|
print 'instance.id is not None: %s' % (instance.id != None)
|
|
|
|
def post_delete_test(signal, sender, instance, **kwargs):
|
|
print 'post_delete signal,', instance
|
|
print 'instance.id is None: %s' % (instance.id == None)
|
|
|
|
__test__ = {'API_TESTS':"""
|
|
>>> models.signals.pre_save.connect(pre_save_test)
|
|
>>> models.signals.post_save.connect(post_save_test)
|
|
>>> models.signals.pre_delete.connect(pre_delete_test)
|
|
>>> models.signals.post_delete.connect(post_delete_test)
|
|
|
|
>>> p1 = Person(first_name='John', last_name='Smith')
|
|
>>> p1.save()
|
|
pre_save signal, John Smith
|
|
post_save signal, John Smith
|
|
Is created
|
|
|
|
>>> p1.first_name = 'Tom'
|
|
>>> p1.save()
|
|
pre_save signal, Tom Smith
|
|
post_save signal, Tom Smith
|
|
Is updated
|
|
|
|
# Calling an internal method purely so that we can trigger a "raw" save.
|
|
>>> p1.save_base(raw=True)
|
|
pre_save signal, Tom Smith
|
|
Is raw
|
|
post_save signal, Tom Smith
|
|
Is updated
|
|
Is raw
|
|
|
|
>>> p1.delete()
|
|
pre_delete signal, Tom Smith
|
|
instance.id is not None: True
|
|
post_delete signal, Tom Smith
|
|
instance.id is None: False
|
|
|
|
>>> p2 = Person(first_name='James', last_name='Jones')
|
|
>>> p2.id = 99999
|
|
>>> p2.save()
|
|
pre_save signal, James Jones
|
|
post_save signal, James Jones
|
|
Is created
|
|
|
|
>>> p2.id = 99998
|
|
>>> p2.save()
|
|
pre_save signal, James Jones
|
|
post_save signal, James Jones
|
|
Is created
|
|
|
|
>>> p2.delete()
|
|
pre_delete signal, James Jones
|
|
instance.id is not None: True
|
|
post_delete signal, James Jones
|
|
instance.id is None: False
|
|
|
|
>>> Person.objects.all()
|
|
[<Person: James Jones>]
|
|
|
|
>>> models.signals.post_delete.disconnect(post_delete_test)
|
|
>>> models.signals.pre_delete.disconnect(pre_delete_test)
|
|
>>> models.signals.post_save.disconnect(post_save_test)
|
|
>>> models.signals.pre_save.disconnect(pre_save_test)
|
|
"""}
|