2007-09-15 17:14:51 +08:00
|
|
|
"""
|
|
|
|
Testing signals before/after saving and deleting.
|
|
|
|
"""
|
|
|
|
|
|
|
|
from django.db import models
|
|
|
|
from django.dispatch import dispatcher
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
2008-02-02 01:32:29 +08:00
|
|
|
def pre_save_nokwargs_test(sender, instance):
|
|
|
|
print 'pre_save_nokwargs signal'
|
|
|
|
|
|
|
|
def post_save_nokwargs_test(sender, instance):
|
|
|
|
print 'post_save_nokwargs signal'
|
|
|
|
|
2007-09-15 17:14:51 +08:00
|
|
|
def pre_save_test(sender, instance, **kwargs):
|
|
|
|
print 'pre_save signal,', instance
|
2008-02-02 01:32:29 +08:00
|
|
|
if kwargs.get('raw'):
|
|
|
|
print 'Is raw'
|
2007-09-15 17:14:51 +08:00
|
|
|
|
|
|
|
def post_save_test(sender, instance, **kwargs):
|
|
|
|
print 'post_save signal,', instance
|
|
|
|
if 'created' in kwargs:
|
|
|
|
if kwargs['created']:
|
|
|
|
print 'Is created'
|
|
|
|
else:
|
|
|
|
print 'Is updated'
|
2008-02-02 01:32:29 +08:00
|
|
|
if kwargs.get('raw'):
|
|
|
|
print 'Is raw'
|
2007-09-15 17:14:51 +08:00
|
|
|
|
|
|
|
def pre_delete_test(sender, instance, **kwargs):
|
|
|
|
print 'pre_delete signal,', instance
|
2008-02-02 01:32:29 +08:00
|
|
|
print 'instance.id is not None: %s' % (instance.id != None)
|
2007-09-15 17:14:51 +08:00
|
|
|
|
|
|
|
def post_delete_test(sender, instance, **kwargs):
|
|
|
|
print 'post_delete signal,', instance
|
2008-02-02 01:32:29 +08:00
|
|
|
print 'instance.id is None: %s' % (instance.id == None)
|
2007-09-15 17:14:51 +08:00
|
|
|
|
|
|
|
__test__ = {'API_TESTS':"""
|
2008-02-02 01:32:29 +08:00
|
|
|
>>> dispatcher.connect(pre_save_nokwargs_test, signal=models.signals.pre_save)
|
|
|
|
>>> dispatcher.connect(post_save_nokwargs_test, signal=models.signals.post_save)
|
2007-09-15 17:14:51 +08:00
|
|
|
>>> 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)
|
|
|
|
>>> dispatcher.connect(post_delete_test, signal=models.signals.post_delete)
|
|
|
|
|
|
|
|
>>> p1 = Person(first_name='John', last_name='Smith')
|
|
|
|
>>> p1.save()
|
2008-02-02 01:32:29 +08:00
|
|
|
pre_save_nokwargs signal
|
2007-09-15 17:14:51 +08:00
|
|
|
pre_save signal, John Smith
|
2008-02-02 01:32:29 +08:00
|
|
|
post_save_nokwargs signal
|
2007-09-15 17:14:51 +08:00
|
|
|
post_save signal, John Smith
|
|
|
|
Is created
|
|
|
|
|
|
|
|
>>> p1.first_name = 'Tom'
|
|
|
|
>>> p1.save()
|
2008-02-02 01:32:29 +08:00
|
|
|
pre_save_nokwargs signal
|
|
|
|
pre_save signal, Tom Smith
|
|
|
|
post_save_nokwargs signal
|
|
|
|
post_save signal, Tom Smith
|
|
|
|
Is updated
|
|
|
|
|
Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7477 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-04-27 10:50:16 +08:00
|
|
|
# Calling an internal method purely so that we can trigger a "raw" save.
|
|
|
|
>>> p1.save_base(raw=True)
|
2008-02-02 01:32:29 +08:00
|
|
|
pre_save_nokwargs signal
|
2007-09-15 17:14:51 +08:00
|
|
|
pre_save signal, Tom Smith
|
2008-02-02 01:32:29 +08:00
|
|
|
Is raw
|
|
|
|
post_save_nokwargs signal
|
2007-09-15 17:14:51 +08:00
|
|
|
post_save signal, Tom Smith
|
|
|
|
Is updated
|
2008-02-02 01:32:29 +08:00
|
|
|
Is raw
|
2007-09-15 17:14:51 +08:00
|
|
|
|
|
|
|
>>> p1.delete()
|
|
|
|
pre_delete signal, Tom Smith
|
|
|
|
instance.id is not None: True
|
|
|
|
post_delete signal, Tom Smith
|
2007-09-25 02:27:42 +08:00
|
|
|
instance.id is None: False
|
2007-09-15 17:14:51 +08:00
|
|
|
|
|
|
|
>>> p2 = Person(first_name='James', last_name='Jones')
|
|
|
|
>>> p2.id = 99999
|
|
|
|
>>> p2.save()
|
2008-02-02 01:32:29 +08:00
|
|
|
pre_save_nokwargs signal
|
2007-09-15 17:14:51 +08:00
|
|
|
pre_save signal, James Jones
|
2008-02-02 01:32:29 +08:00
|
|
|
post_save_nokwargs signal
|
2007-09-15 17:14:51 +08:00
|
|
|
post_save signal, James Jones
|
|
|
|
Is created
|
|
|
|
|
|
|
|
>>> p2.id = 99998
|
|
|
|
>>> p2.save()
|
2008-02-02 01:32:29 +08:00
|
|
|
pre_save_nokwargs signal
|
2007-09-15 17:14:51 +08:00
|
|
|
pre_save signal, James Jones
|
2008-02-02 01:32:29 +08:00
|
|
|
post_save_nokwargs signal
|
2007-09-15 17:14:51 +08:00
|
|
|
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
|
2007-09-25 02:27:42 +08:00
|
|
|
instance.id is None: False
|
2007-09-15 17:14:51 +08:00
|
|
|
|
|
|
|
>>> Person.objects.all()
|
|
|
|
[<Person: James Jones>]
|
|
|
|
|
2008-02-02 01:32:29 +08:00
|
|
|
>>> dispatcher.disconnect(pre_save_nokwargs_test, signal=models.signals.pre_save)
|
|
|
|
>>> dispatcher.disconnect(post_save_nokwargs_test, signal=models.signals.post_save)
|
2007-09-15 17:14:51 +08:00
|
|
|
>>> 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)
|
|
|
|
>>> dispatcher.disconnect(pre_save_test, signal=models.signals.pre_save)
|
|
|
|
"""}
|