3
=========================
4
Built-in signal reference
5
=========================
7
A list of all the signals that Django sends.
11
The :ref:`comment framework <ref-contrib-comments-index>` sends a :ref:`set
12
of comment-related signals <ref-contrib-comments-signals>`.
17
.. module:: django.db.models.signals
18
:synopsis: Signals sent by the model system.
20
The :mod:`django.db.models.signals` module defines a set of signals sent by the
25
Many of these signals are sent by various model methods like
26
:meth:`~django.db.models.Model.__init__` or
27
:meth:`~django.db.models.Model.save` that you can overwrite in your own
30
If you override these methods on your model, you must call the parent class'
31
methods for this signals to be sent.
36
.. attribute:: django.db.models.signals.pre_init
39
.. ^^^^^^^ this :module: hack keeps Sphinx from prepending the module.
41
Whenever you instantiate a Django model,, this signal is sent at the beginning
42
of the model's :meth:`~django.db.models.Model.__init__` method.
44
Arguments sent with this signal:
47
The model class that just had an instance created.
50
A list of positional arguments passed to
51
:meth:`~django.db.models.Model.__init__`:
54
A dictionary of keyword arguments passed to
55
:meth:`~django.db.models.Model.__init__`:.
57
For example, the :ref:`tutorial <intro-tutorial01>` has this line:
59
.. code-block:: python
61
p = Poll(question="What's up?", pub_date=datetime.now())
63
The arguments sent to a :data:`pre_init` handler would be:
65
========== ===============================================================
67
========== ===============================================================
68
``sender`` ``Poll`` (the class itself)
70
``args`` ``[]`` (an empty list because there were no positional
71
arguments passed to ``__init__``.)
73
``kwargs`` ``{'question': "What's up?", 'pub_date': datetime.now()}``
74
========== ===============================================================
79
.. data:: django.db.models.signals.post_init
82
Like pre_init, but this one is sent when the :meth:`~django.db.models.Model.__init__`: method finishes.
84
Arguments sent with this signal:
87
As above: rhe model class that just had an instance created.
90
The actual instance of the model that's just been created.
95
.. data:: django.db.models.signals.pre_save
98
This is sent at the beginning of a model's :meth:`~django.db.models.Model.save`
101
Arguments sent with this signal:
107
The actual instance being saved.
112
.. data:: django.db.models.signals.post_save
115
Like :data:`pre_save`, but sent at the end of the
116
:meth:`~django.db.models.Model.save` method.
118
Arguments sent with this signal:
124
The actual instance being saved.
127
A boolean; ``True`` if a new record was create.
132
.. data:: django.db.models.signals.pre_delete
135
Sent at the beginning of a model's :meth:`~django.db.models.Model.delete`
138
Arguments sent with this signal:
144
The actual instance being saved.
149
.. data:: django.db.models.signals.post_delete
152
Like :data:`pre_delete`, but sent at the end of the
153
:meth:`~django.db.models.Model.delete` method.
155
Arguments sent with this signal:
161
The actual instance being saved.
163
Note that the object will no longer be in the database, so be very
164
careful what you do with this instance
169
.. data:: django.db.models.signals.class_prepared
172
Sent whenever a model class has been "prepared" -- that is, once model has
173
been defined and registered with Django's model system. Django uses this
174
signal internally; it's not generally used in third-party applications.
176
Arguments that are sent with this signal:
179
The model class which was just prepared.
184
Signals sent by :ref:`django-admin <ref-django-admin>`.
189
.. data:: django.db.models.signals.post_syncdb
192
Sent by :djadmin:`syncdb` after it installs an application.
194
Any handlers that listen to this signal need to be written in a particular
195
place: a ``management`` module in one of your :setting:`INSTALLED_APPS`. If
196
handlers are registered anywhere else they may not be loaded by
199
Arguments sent with this signal:
202
The ``models`` module that was just installed. That is, if
203
:djadmin:`syncdb` just installed an app called ``"foo.bar.myapp"``,
204
``sender`` will be the ``foo.bar.myapp.models`` module.
210
A list of the model classes from any app which :djadmin:`syncdb` has
214
Indicates how much information manage.py is printing on screen. See
215
the :djadminopt:`--verbosity`` flag for details.
217
Functions which listen for :data:`post_syncdb` should adjust what they
218
output to the screen based on the value of this argument.
221
If ``interactive`` is ``True``, it's safe to prompt the user to input
222
things on the command line. If ``interactive`` is ``False``, functions
223
which listen for this signal should not try to prompt for anything.
225
For example, the :mod:`django.contrib.auth` app only prompts to create a
226
superuser when ``interactive`` is ``True``.
228
Request/response signals
229
========================
231
.. module:: django.core.signals
232
:synopsis: Core signals sent by the request/response system.
234
Signals sent by the core framework when processing a request.
239
.. data:: django.core.signals.request_started
242
Sent when Django begins processing an HTTP request.
244
Arguments sent with this signal:
247
The handler class -- i.e.
248
:class:`django.core.handlers.modpython.ModPythonHandler` or
249
:class:`django.core.handlers.wsgi.WsgiHandler` -- that handled
255
.. data:: django.core.signals.request_finished
258
Sent when Django finishes processing an HTTP request.
260
Arguments sent with this signal:
263
The handler class, as above.
265
got_request_exception
266
---------------------
268
.. data:: django.core.signals.got_request_exception
271
This signal is sent whenever Django encounters an exception while processing an incoming HTTP request.
273
Arguments sent with this signal:
276
The handler class, as above.
279
The :class:`~django.http.HttpRequest` object.
284
.. module:: django.test.signals
285
:synopsis: Signals sent during testing.
287
Signals only sent when :ref:`running tests <topics-testing>`.
292
.. data:: django.test.signals.template_rendered
295
Sent when the test system renders a template. This signal is not emitted during
296
normal operation of a Django server -- it is only available during testing.
298
Arguments sent with this signal:
301
The :class:`~django.template.Template` object which was rendered.
307
The :class:`~django.template.Context` with which the template was