From b7f60045fe9e662c8c53a6f7b4e2c410830d5b29 Mon Sep 17 00:00:00 2001 From: Brian Rosner Date: Sun, 12 Sep 2010 19:58:05 +0000 Subject: Fixed #9015 -- added a signal decorator for simplifying signal connections git-svn-id: http://code.djangoproject.com/svn/django/trunk@13773 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- docs/topics/signals.txt | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) (limited to 'docs') diff --git a/docs/topics/signals.txt b/docs/topics/signals.txt index 35dc3f4c09..78790db071 100644 --- a/docs/topics/signals.txt +++ b/docs/topics/signals.txt @@ -80,7 +80,8 @@ must be able to handle those new arguments. Connecting receiver functions ----------------------------- -Next, we'll need to connect our receiver to the signal: +There are two ways you can connect a receiever to a signal. You can take the +manual connect route: .. code-block:: python @@ -88,6 +89,17 @@ Next, we'll need to connect our receiver to the signal: request_finished.connect(my_callback) +Alternatively, you can use a decorator used when you define your receiver: + +.. code-block:: python + + from django.core.signals import request_finished + from django.dispatch import receiver + + @receiver(request_finished) + def my_callback(sender, **kwargs): + print "Request finished!" + Now, our ``my_callback`` function will be called each time a request finishes. .. admonition:: Where should this code live? @@ -115,13 +127,13 @@ signals sent by some model: .. code-block:: python from django.db.models.signals import pre_save + from django.dispatch import receiver from myapp.models import MyModel + @receiver(pre_save, sender=MyModel) def my_handler(sender, **kwargs): ... - pre_save.connect(my_handler, sender=MyModel) - The ``my_handler`` function will only be called when an instance of ``MyModel`` is saved. -- cgit v1.3