Changes between Version 8 and Version 9 of Signals
- Timestamp:
- Oct 4, 2006, 10:58:31 PM (18 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
Signals
v8 v9 12 12 A 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: 13 13 14 {{{ post_save = object() }}} 14 {{{ 15 post_save = object() 16 }}} 15 17 16 18 When a piece of code wants to send a signal, it needs to do three things: … … 22 24 An 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: 23 25 24 {{{ from django.db.models import signals }}} 26 {{{ 27 from django.db.models import signals 28 }}} 25 29 26 30 It also imports the dispatcher: 27 31 28 {{{ from django.dispatch import dispatcher }}} 32 {{{ 33 from django.dispatch import dispatcher 34 }}} 29 35 30 36 And in the very last line of the `save` method, it sends the `post_save` signal: 31 37 32 {{{ dispatcher.send(signal=signals.post_save, sender=self.__class__, instance=self) }}} 38 {{{ 39 dispatcher.send(signal=signals.post_save, sender=self.__class__, instance=self) 40 }}} 33 41 34 42 The first two arguments are fairly clear, and are common for all uses of the dispatcher: … … 51 59 The `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: 52 60 53 {{{ dispatcher.connect(create_contenttypes, signal=signals.post_syncdb) }}} 61 {{{ 62 dispatcher.connect(create_contenttypes, signal=signals.post_syncdb) 63 }}} 54 64 55 65 The 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. … … 57 67 An 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: 58 68 59 {{{ from django.contrib.auth import models as auth_app }}} 69 {{{ 70 from django.contrib.auth import models as auth_app 71 }}} 60 72 61 73 And then uses the `sender` argument to `dispatcher.connect`: 62 74 63 {{{ dispatcher.connect(create_superuser, sender=auth_app, signal=signals.post_syncdb) }}} 75 {{{ 76 dispatcher.connect(create_superuser, sender=auth_app, signal=signals.post_syncdb) 77 }}} 64 78 65 79 Here's a breakdown of exactly why that works: … … 104 118 * `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. 105 119 106 '' pre_save'''120 '''pre_save''' 107 121 108 122 This is sent at the beginning of a model's `save` method. … … 152 166 * `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. 153 167 168 ---- 169 154 170 `django.core.signals` defines the following signals: 155 171 … … 171 187 172 188 This signal doesn't provide any arguments. 189 190 ---- 173 191 174 192 `django.test.signals` defines the following signals: