diff options
Diffstat (limited to 'docs/topics/signals.txt')
-rw-r--r-- | docs/topics/signals.txt | 26 |
1 files changed, 18 insertions, 8 deletions
diff --git a/docs/topics/signals.txt b/docs/topics/signals.txt index eb1a5fd825..78790db071 100644 --- a/docs/topics/signals.txt +++ b/docs/topics/signals.txt @@ -1,5 +1,3 @@ -.. _topics-signals: - ======= Signals ======= @@ -13,7 +11,7 @@ signals allow certain *senders* to notify a set of *receivers* that some action has taken place. They're especially useful when many pieces of code may be interested in the same events. -Django provides a :ref:`set of built-in signals <ref-signals>` that let user +Django provides a :doc:`set of built-in signals </ref/signals>` that let user code get notified by Django itself of certain actions. These include some useful notifications: @@ -38,7 +36,7 @@ notifications: Sent when Django starts or finishes an HTTP request. -See the :ref:`built-in signal documentation <ref-signals>` for a complete list, +See the :doc:`built-in signal documentation </ref/signals>` for a complete list, and a complete explanation of each signal. You can also `define and send your own custom signals`_; see below. @@ -82,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 @@ -90,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? @@ -117,18 +127,18 @@ 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. Different signals use different objects as their senders; you'll need to consult -the :ref:`built-in signal documentation <ref-signals>` for details of each +the :doc:`built-in signal documentation </ref/signals>` for details of each particular signal. Defining and sending signals |