Changes between Version 25 and Version 26 of Signals
- Timestamp:
- Aug 8, 2008, 8:22:33 AM (16 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
Signals
v25 v26 8 8 2. Pieces of code which want to do something whenever a certain event happens can use the dispatcher to "listen" for particular signals, then execute the functions they want when those signals are sent out. 9 9 10 The actual mechanism comes from a third-party library called [http://pydispatcher.sourceforge.net/ PyDispatcher], which is bundled with Django and which lives at `django.dispatch.dispatcher`. 10 The actual mechanism comes from a third-party library called [http://pydispatcher.sourceforge.net/ PyDispatcher], which is bundled with Django and which lives at `django.dispatch.dispatcher`. To improve performance the API and implementation has been refactored, as described at the end of this document. 11 11 12 12 == How signals work == … … 15 15 16 16 {{{ 17 post_save = object()17 post_save = django.dispatch.Signal() 18 18 }}} 19 19 … … 21 21 22 22 1. Import the signal from wherever it's been defined. 23 2. Import the dispatcher. 24 3. Use the dispatcher's `send` function to send the signal. 23 2. Use the signal's `send` function to send the signal. 25 24 26 25 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: … … 30 29 }}} 31 30 32 It also imports the dispatcher:33 34 {{{35 from django.dispatch import dispatcher36 }}}37 38 31 And in the very last line of the `save` method, it sends the `post_save` signal: 39 32 40 33 {{{ 41 dispatcher.send(signal=signals.post_save, sender=self.__class__, instance=self) 42 }}} 43 44 The first two arguments are fairly clear, and are common for all uses of the dispatcher: 45 46 1. `signal` is the signal to be sent. 47 2. `sender` is the object which is sending the signal; in this case, `self.__class__`; this will be whatever model class the object which was just saved belongs to. 48 49 The third argument, `instance`, is one of the more interesting features of the dispatcher: when sending a signal, you are free to include ''any'' extra arguments beyond these two; if a function which is listening for the signal is expecting certain information to be passed in as arguments, the dispatcher will ensure that the extra arguments to `send` are used. In this case the extra argument `instance` is `self`, which means it will be the object which was just saved. This means that functions which are listening for the `post_save` signal can, if they want to do something with the object that was just saved, specify that they take an argument named `instance`, and the dispatcher will pass it to them. 34 signals.post_save.send(sender=self.__class__, instance=self) 35 }}} 36 37 The first argument is fairly clear, and is common for all uses of the dispatcher; `sender` is the object which is sending the signal; in this case, `self.__class__`; this will be whatever model class the object which was just saved belongs to. 38 39 The second argument, `instance`, is one of the more interesting features of the dispatcher: when sending a signal, you are free to include ''any'' extra arguments beyond the first; if a function which is listening for the signal is expecting certain information to be passed in as arguments, the dispatcher will ensure that the extra arguments to `send` are used. In this case the extra argument `instance` is `self`, which means it will be the object which was just saved. This means that functions which are listening for the `post_save` signal can, if they want to do something with the object that was just saved, specify that they take an argument named `instance`. 50 40 51 41 To listen for a signal, first define a function that you want to execute when the signal is sent; if you know that the signal will be sent with extra arguments and you want to use those arguments, make sure your function accepts them. Then you need to do three things: 52 42 53 43 1. Make sure there is a reference to your signal handler somewhere. If it is defined as a local function, chances are it will be garbage collected and won't receive any signals. 54 2. Import the signal object you'll be listening for.55 3. Import the dispatcher.56 4. Use the dispatcher's `connect` signalto tell the dispatcher you want to listen for something.44 2. Make sure that the signal handler has **kwargs as the last parameter. 45 3. Import the signal object you'll be listening for. 46 4. Use the signal's `connect` method to tell the dispatcher you want to listen for something. 57 47 58 48 A good example of this is found in Django's bundled "contenttypes" application, which creates and maintains a registry of all the installed models in your database. In order to do this, the contenttypes app defines a `ContentType` model, and it needs to know any time a new model is installed so it can create the appropriate `ContentType` object for that model. To do this, it includes a file called `management.py`; whenever `manage.py syncdb` is run, it loops through ''every'' application in the `INSTALLED_APPS` setting, and looks to see if any apps contain a module called `management`; if they do, `manage.py` imports them before installing any models, which means that any dispatcher connections listed in an app's `management` module will be set up before model installation happens. … … 63 53 64 54 {{{ 65 dispatcher.connect(update_contenttypes, signal=signals.post_syncdb)66 }}} 67 68 The first argument, `update_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.55 signals.post_syncdb.connect(update_contenttypes) 56 }}} 57 58 The method is called on an instance of django.dispatch.Signal, identifying the signal to listen for. The first argument, `update_contenttypes`, is a reference to the function to execute when the signal is sent out. 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. 69 59 70 60 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: … … 74 64 }}} 75 65 76 And then uses the `sender` argument to ` dispatcher.connect`:77 78 {{{ 79 dispatcher.connect(create_superuser, sender=auth_app, signal=signals.post_syncdb)66 And then uses the `sender` argument to `Signal.connect`: 67 68 {{{ 69 signals.post_syncdb.connect(create_superuser, sender=auth_app) 80 70 }}} 81 71