1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
|
.. _ref-signals:
=========================
Built-in signal reference
=========================
A list of all the signals that Django sends.
.. seealso::
The :ref:`comment framework <ref-contrib-comments-index>` sends a :ref:`set
of comment-related signals <ref-contrib-comments-signals>`.
Model signals
=============
.. module:: django.db.models.signals
:synopsis: Signals sent by the model system.
The :mod:`django.db.models.signals` module defines a set of signals sent by the
module system.
.. warning::
Many of these signals are sent by various model methods like
:meth:`~django.db.models.Model.__init__` or
:meth:`~django.db.models.Model.save` that you can overwrite in your own
code.
If you override these methods on your model, you must call the parent class'
methods for this signals to be sent.
pre_init
--------
.. attribute:: django.db.models.signals.pre_init
:module:
.. ^^^^^^^ this :module: hack keeps Sphinx from prepending the module.
Whenever you instantiate a Django model,, this signal is sent at the beginning
of the model's :meth:`~django.db.models.Model.__init__` method.
Arguments sent with this signal:
``sender``
The model class that just had an instance created.
``args``
A list of positional arguments passed to
:meth:`~django.db.models.Model.__init__`:
``kwargs``
A dictionary of keyword arguments passed to
:meth:`~django.db.models.Model.__init__`:.
For example, the :ref:`tutorial <intro-tutorial01>` has this line:
.. code-block:: python
p = Poll(question="What's up?", pub_date=datetime.now())
The arguments sent to a :data:`pre_init` handler would be:
========== ===============================================================
Argument Value
========== ===============================================================
``sender`` ``Poll`` (the class itself)
``args`` ``[]`` (an empty list because there were no positional
arguments passed to ``__init__``.)
``kwargs`` ``{'question': "What's up?", 'pub_date': datetime.now()}``
========== ===============================================================
post_init
---------
.. data:: django.db.models.signals.post_init
:module:
Like pre_init, but this one is sent when the :meth:`~django.db.models.Model.__init__`: method finishes.
Arguments sent with this signal:
``sender``
As above: the model class that just had an instance created.
``instance``
The actual instance of the model that's just been created.
pre_save
--------
.. data:: django.db.models.signals.pre_save
:module:
This is sent at the beginning of a model's :meth:`~django.db.models.Model.save`
method.
Arguments sent with this signal:
``sender``
The model class.
``instance``
The actual instance being saved.
post_save
---------
.. data:: django.db.models.signals.post_save
:module:
Like :data:`pre_save`, but sent at the end of the
:meth:`~django.db.models.Model.save` method.
Arguments sent with this signal:
``sender``
The model class.
``instance``
The actual instance being saved.
``created``
A boolean; ``True`` if a new record was create.
pre_delete
----------
.. data:: django.db.models.signals.pre_delete
:module:
Sent at the beginning of a model's :meth:`~django.db.models.Model.delete`
method.
Arguments sent with this signal:
``sender``
The model class.
``instance``
The actual instance being deleted.
post_delete
-----------
.. data:: django.db.models.signals.post_delete
:module:
Like :data:`pre_delete`, but sent at the end of the
:meth:`~django.db.models.Model.delete` method.
Arguments sent with this signal:
``sender``
The model class.
``instance``
The actual instance being deleted.
Note that the object will no longer be in the database, so be very
careful what you do with this instance.
class_prepared
--------------
.. data:: django.db.models.signals.class_prepared
:module:
Sent whenever a model class has been "prepared" -- that is, once model has
been defined and registered with Django's model system. Django uses this
signal internally; it's not generally used in third-party applications.
Arguments that are sent with this signal:
``sender``
The model class which was just prepared.
Management signals
==================
Signals sent by :ref:`django-admin <ref-django-admin>`.
post_syncdb
-----------
.. data:: django.db.models.signals.post_syncdb
:module:
Sent by :djadmin:`syncdb` after it installs an application.
Any handlers that listen to this signal need to be written in a particular
place: a ``management`` module in one of your :setting:`INSTALLED_APPS`. If
handlers are registered anywhere else they may not be loaded by
:djadmin:`syncdb`.
Arguments sent with this signal:
``sender``
The ``models`` module that was just installed. That is, if
:djadmin:`syncdb` just installed an app called ``"foo.bar.myapp"``,
``sender`` will be the ``foo.bar.myapp.models`` module.
``app``
Same as ``sender``.
``created_models``
A list of the model classes from any app which :djadmin:`syncdb` has
created so far.
``verbosity``
Indicates how much information manage.py is printing on screen. See
the :djadminopt:`--verbosity`` flag for details.
Functions which listen for :data:`post_syncdb` should adjust what they
output to the screen based on the value of this argument.
``interactive``
If ``interactive`` is ``True``, it's safe to prompt the user to input
things on the command line. If ``interactive`` is ``False``, functions
which listen for this signal should not try to prompt for anything.
For example, the :mod:`django.contrib.auth` app only prompts to create a
superuser when ``interactive`` is ``True``.
Request/response signals
========================
.. module:: django.core.signals
:synopsis: Core signals sent by the request/response system.
Signals sent by the core framework when processing a request.
request_started
---------------
.. data:: django.core.signals.request_started
:module:
Sent when Django begins processing an HTTP request.
Arguments sent with this signal:
``sender``
The handler class -- i.e.
:class:`django.core.handlers.modpython.ModPythonHandler` or
:class:`django.core.handlers.wsgi.WsgiHandler` -- that handled
the request.
request_finished
----------------
.. data:: django.core.signals.request_finished
:module:
Sent when Django finishes processing an HTTP request.
Arguments sent with this signal:
``sender``
The handler class, as above.
got_request_exception
---------------------
.. data:: django.core.signals.got_request_exception
:module:
This signal is sent whenever Django encounters an exception while processing an incoming HTTP request.
Arguments sent with this signal:
``sender``
The handler class, as above.
``request``
The :class:`~django.http.HttpRequest` object.
Test signals
============
.. module:: django.test.signals
:synopsis: Signals sent during testing.
Signals only sent when :ref:`running tests <topics-testing>`.
template_rendered
-----------------
.. data:: django.test.signals.template_rendered
:module:
Sent when the test system renders a template. This signal is not emitted during
normal operation of a Django server -- it is only available during testing.
Arguments sent with this signal:
sender
The :class:`~django.template.Template` object which was rendered.
template
Same as sender
context
The :class:`~django.template.Context` with which the template was
rendered.
|