| | 1 | == What Are Signals? == |
| | 2 | |
| | 3 | Signals allow you to make Django call custom methods after common actions using a dispatcher. Beyond that, I really don't know much else. Please help expand this documentation, if you can. |
| | 4 | |
| | 5 | The author of Zyons [http://feh.holsman.net/articles/2006/06/13/django-signals talks about signals a bit here]. |
| | 6 | |
| | 7 | == Example == |
| | 8 | |
| | 9 | The following is taken from [http://zyons.com/ Zyons], the only place I've seen signals used so far. Unless I'm completely incorrect, the following code has the dispatcher call "increment_tag_summary" before a TagUserObject object is saved, and "decrement_tag_summary" after a TagUserObject object is deleted. |
| | 10 | |
| | 11 | === models.py === |
| | 12 | |
| | 13 | {{{ |
| | 14 | from django.db.models import signals |
| | 15 | from django.dispatch import dispatcher |
| | 16 | |
| | 17 | [...] |
| | 18 | |
| | 19 | def increment_tag_summary(sender, instance, signal, *args, **kwargs): |
| | 20 | [...] |
| | 21 | |
| | 22 | def decrement_tag_summary(sender, instance, signal, *args, **kwargs): |
| | 23 | [...] |
| | 24 | |
| | 25 | dispatcher.connect( increment_tag_summary , signal=signals.pre_save, sender=TagUserObject ) |
| | 26 | dispatcher.connect( decrement_tag_summary , signal=signals.post_delete, sender=TagUserObject ) |
| | 27 | }}} |