Changes between Version 8 and Version 9 of Signals


Ignore:
Timestamp:
Oct 4, 2006, 10:58:31 PM (18 years ago)
Author:
James Bennett
Comment:

Formatting

Legend:

Unmodified
Added
Removed
Modified
  • Signals

    v8 v9  
    1212A signal is just a simple Python object, and requires nothing special to set up. For example, the `post_save` signal, found in `django.db.models.signals`, is defined like so:
    1313
    14 {{{ post_save = object() }}}
     14{{{
     15post_save = object()
     16}}}
    1517
    1618When a piece of code wants to send a signal, it needs to do three things:
     
    2224An example of this can be found in the `save` method of the base model class, `django.db.models.Model`; the file in which that class is defined imports the signals defined in the `django.db.models` module:
    2325
    24 {{{ from django.db.models import signals }}}
     26{{{
     27from django.db.models import signals
     28}}}
    2529
    2630It also imports the dispatcher:
    2731
    28 {{{ from django.dispatch import dispatcher }}}
     32{{{
     33from django.dispatch import dispatcher
     34}}}
    2935
    3036And in the very last line of the `save` method, it sends the `post_save` signal:
    3137
    32 {{{ dispatcher.send(signal=signals.post_save, sender=self.__class__, instance=self) }}}
     38{{{
     39dispatcher.send(signal=signals.post_save, sender=self.__class__, instance=self)
     40}}}
    3341
    3442The first two arguments are fairly clear, and are common for all uses of the dispatcher:
     
    5159The `management.py` file also imports `django.db.models.signals` and `django.dispatch.dispatcher`; after the `create_contenttypes` function is defined, it sets up that function to listen for the `post_syncdb` signal:
    5260
    53 {{{ dispatcher.connect(create_contenttypes, signal=signals.post_syncdb) }}}
     61{{{
     62dispatcher.connect(create_contenttypes, signal=signals.post_syncdb)
     63}}}
    5464
    5565The first argument, `create_contenttypes`, is the name of the function to execute when the signal is sent out. The second argument, `signal`, is the signal to listen for. There is another optional argument, `sender`, which is not used in this example; `sender` can be used to narrow down exactly what will be listened for; when you specify `sender`, your function will only be executed when the object which sent the signal is the same as the object you specify as the `sender` argument.
     
    5767An example of this can be found in Django's authentication application: `django.contrib.auth.management` defines a function called `create_superuser`, and uses the dispatcher to connect to the `post_syncdb` signal -- but ''only'' when `post_syncdb` is being sent as a result of installing the auth application. To do this, the auth app's `management.py` file imports its own models:
    5868
    59 {{{ from django.contrib.auth import models as auth_app }}}
     69{{{
     70from django.contrib.auth import models as auth_app
     71}}}
    6072
    6173And then uses the `sender` argument to `dispatcher.connect`:
    6274
    63 {{{ dispatcher.connect(create_superuser, sender=auth_app, signal=signals.post_syncdb) }}}
     75{{{
     76dispatcher.connect(create_superuser, sender=auth_app, signal=signals.post_syncdb)
     77}}}
    6478
    6579Here's a breakdown of exactly why that works:
     
    104118 * `instance` -- the instance of the model you just created. For example, in the tutorial when you do `p = Poll(question="What's up?", pub_date=datetime.now())`, the `instance` argument to the `post_init` signal would be the `Poll` object you just created.
    105119
    106 ''pre_save'''
     120'''pre_save'''
    107121
    108122This is sent at the beginning of a model's `save` method.
     
    152166 * `interactive` -- whether `manage.py` is running in "interactive" mode; this is a boolean and so is either `True` or `False`. If `interactive` is `True`, it's safe to prompt the user to input things on the command line (for example, the auth app only prompts to create a superuser when `interactive` is `True`); if `interactive` is `False`, functions which listen for this signal should not try to prompt for anything.
    153167
     168----
     169
    154170`django.core.signals` defines the following signals:
    155171
     
    171187
    172188This signal doesn't provide any arguments.
     189
     190----
    173191
    174192`django.test.signals` defines the following signals:
Back to Top