5 A list of all the signals that Django sends.
9 See the documentation on the :doc:`signal dispatcher </topics/signals>` for
10 information regarding how to register for and receive signals.
12 The :doc:`comment framework </ref/contrib/comments/index>` sends a :doc:`set
13 of comment-related signals </ref/contrib/comments/signals>`.
15 The :doc:`authentication framework </topics/auth>` sends :ref:`signals when
16 a user is logged in / out <topics-auth-signals>`.
21 .. module:: django.db.models.signals
22 :synopsis: Signals sent by the model system.
24 The :mod:`django.db.models.signals` module defines a set of signals sent by the
29 Many of these signals are sent by various model methods like
30 :meth:`~django.db.models.Model.__init__` or
31 :meth:`~django.db.models.Model.save` that you can overwrite in your own
34 If you override these methods on your model, you must call the parent class'
35 methods for this signals to be sent.
37 Note also that Django stores signal handlers as weak references by default,
38 so if your handler is a local function, it may be garbage collected. To
39 prevent this, pass ``weak=False`` when you call the signal's :meth:`~django.dispatch.Signal.connect`.
44 .. attribute:: django.db.models.signals.pre_init
47 .. ^^^^^^^ this :module: hack keeps Sphinx from prepending the module.
49 Whenever you instantiate a Django model,, this signal is sent at the beginning
50 of the model's :meth:`~django.db.models.Model.__init__` method.
52 Arguments sent with this signal:
55 The model class that just had an instance created.
58 A list of positional arguments passed to
59 :meth:`~django.db.models.Model.__init__`:
62 A dictionary of keyword arguments passed to
63 :meth:`~django.db.models.Model.__init__`:.
65 For example, the :doc:`tutorial </intro/tutorial01>` has this line::
67 p = Poll(question="What's up?", pub_date=datetime.now())
69 The arguments sent to a :data:`pre_init` handler would be:
71 ========== ===============================================================
73 ========== ===============================================================
74 ``sender`` ``Poll`` (the class itself)
76 ``args`` ``[]`` (an empty list because there were no positional
77 arguments passed to ``__init__``.)
79 ``kwargs`` ``{'question': "What's up?", 'pub_date': datetime.now()}``
80 ========== ===============================================================
85 .. data:: django.db.models.signals.post_init
88 Like pre_init, but this one is sent when the :meth:`~django.db.models.Model.__init__`: method finishes.
90 Arguments sent with this signal:
93 As above: the model class that just had an instance created.
96 The actual instance of the model that's just been created.
101 .. data:: django.db.models.signals.pre_save
104 This is sent at the beginning of a model's :meth:`~django.db.models.Model.save`
107 Arguments sent with this signal:
113 The actual instance being saved.
116 A boolean; ``True`` if the model is saved exactly as presented
117 (i.e. when loading a fixture). One should not query/modify other
118 records in the database as the database might not be in a
119 consistent state yet.
121 .. versionadded:: 1.3
124 The database alias being used.
129 .. data:: django.db.models.signals.post_save
132 Like :data:`pre_save`, but sent at the end of the
133 :meth:`~django.db.models.Model.save` method.
135 Arguments sent with this signal:
141 The actual instance being saved.
144 A boolean; ``True`` if a new record was created.
147 A boolean; ``True`` if the model is saved exactly as presented
148 (i.e. when loading a fixture). One should not query/modify other
149 records in the database as the database might not be in a
150 consistent state yet.
152 .. versionadded:: 1.3
155 The database alias being used.
160 .. data:: django.db.models.signals.pre_delete
163 Sent at the beginning of a model's :meth:`~django.db.models.Model.delete`
164 method and a queryset's :meth:`~django.db.models.query.QuerySet.delete` method.
166 Arguments sent with this signal:
172 The actual instance being deleted.
174 .. versionadded:: 1.3
177 The database alias being used.
182 .. data:: django.db.models.signals.post_delete
185 Like :data:`pre_delete`, but sent at the end of a model's
186 :meth:`~django.db.models.Model.delete` method and a queryset's
187 :meth:`~django.db.models.query.QuerySet.delete` method.
189 Arguments sent with this signal:
195 The actual instance being deleted.
197 Note that the object will no longer be in the database, so be very
198 careful what you do with this instance.
200 .. versionadded:: 1.3
203 The database alias being used.
208 .. data:: django.db.models.signals.m2m_changed
211 .. versionadded:: 1.2
213 Sent when a :class:`ManyToManyField` is changed on a model instance.
214 Strictly speaking, this is not a model signal since it is sent by the
215 :class:`ManyToManyField`, but since it complements the
216 :data:`pre_save`/:data:`post_save` and :data:`pre_delete`/:data:`post_delete`
217 when it comes to tracking changes to models, it is included here.
219 Arguments sent with this signal:
222 The intermediate model class describing the :class:`ManyToManyField`.
223 This class is automatically created when a many-to-many field is
224 defined; you can access it using the ``through`` attribute on the
228 The instance whose many-to-many relation is updated. This can be an
229 instance of the ``sender``, or of the class the :class:`ManyToManyField`
233 A string indicating the type of update that is done on the relation.
234 This can be one of the following:
237 Sent *before* one or more objects are added to the relation.
239 Sent *after* one or more objects are added to the relation.
241 Sent *before* one or more objects are removed from the relation.
243 Sent *after* one or more objects are removed from the relation.
245 Sent *before* the relation is cleared.
247 Sent *after* the relation is cleared.
250 Indicates which side of the relation is updated (i.e., if it is the
251 forward or reverse relation that is being modified).
254 The class of the objects that are added to, removed from or cleared
258 For the ``pre_add``, ``post_add``, ``pre_remove`` and ``post_remove``
259 actions, this is a list of primary key values that have been added to
260 or removed from the relation.
262 For the ``pre_clear`` and ``post_clear`` actions, this is ``None``.
264 .. versionadded:: 1.3
267 The database alias being used.
269 For example, if a ``Pizza`` can have multiple ``Topping`` objects, modeled
272 class Topping(models.Model):
276 class Pizza(models.Model):
278 toppings = models.ManyToManyField(Topping)
280 If we connected a handler like this::
282 def toppings_changed(sender, **kwargs):
286 m2m_changed.connect(toppings_changed, sender=Pizza.toppings.through)
288 and then did something like this::
290 >>> p = Pizza.object.create(...)
291 >>> t = Topping.objects.create(...)
292 >>> p.toppings.add(t)
294 the arguments sent to a :data:`m2m_changed` handler (``topppings_changed`` in
295 the example above) would be:
297 ============== ============================================================
299 ============== ============================================================
300 ``sender`` ``Pizza.toppings.through`` (the intermediate m2m class)
302 ``instance`` ``p`` (the ``Pizza`` instance being modified)
304 ``action`` ``"pre_add"`` (followed by a separate signal with ``"post_add"``)
306 ``reverse`` ``False`` (``Pizza`` contains the :class:`ManyToManyField`,
307 so this call modifies the forward relation)
309 ``model`` ``Topping`` (the class of the objects added to the
312 ``pk_set`` ``[t.id]`` (since only ``Topping t`` was added to the relation)
314 ``using`` ``"default"`` (since the default router sends writes here)
315 ============== ============================================================
317 And if we would then do something like this::
319 >>> t.pizza_set.remove(p)
321 the arguments sent to a :data:`m2m_changed` handler would be:
323 ============== ============================================================
325 ============== ============================================================
326 ``sender`` ``Pizza.toppings.through`` (the intermediate m2m class)
328 ``instance`` ``t`` (the ``Topping`` instance being modified)
330 ``action`` ``"pre_remove"`` (followed by a separate signal with ``"post_remove"``)
332 ``reverse`` ``True`` (``Pizza`` contains the :class:`ManyToManyField`,
333 so this call modifies the reverse relation)
335 ``model`` ``Pizza`` (the class of the objects removed from the
338 ``pk_set`` ``[p.id]`` (since only ``Pizza p`` was removed from the
341 ``using`` ``"default"`` (since the default router sends writes here)
342 ============== ============================================================
347 .. data:: django.db.models.signals.class_prepared
350 Sent whenever a model class has been "prepared" -- that is, once model has
351 been defined and registered with Django's model system. Django uses this
352 signal internally; it's not generally used in third-party applications.
354 Arguments that are sent with this signal:
357 The model class which was just prepared.
362 Signals sent by :doc:`django-admin </ref/django-admin>`.
367 .. data:: django.db.models.signals.post_syncdb
370 Sent by the :djadmin:`syncdb` command after it installs an application, and the
371 :djadmin:`flush` command.
373 Any handlers that listen to this signal need to be written in a particular
374 place: a ``management`` module in one of your :setting:`INSTALLED_APPS`. If
375 handlers are registered anywhere else they may not be loaded by
376 :djadmin:`syncdb`. It is important that handlers of this signal perform
377 idempotent changes (e.g. no database alterations) as this may cause the
378 :djadmin:`flush` management command to fail if it also ran during the
379 :djadmin:`syncdb` command.
381 Arguments sent with this signal:
384 The ``models`` module that was just installed. That is, if
385 :djadmin:`syncdb` just installed an app called ``"foo.bar.myapp"``,
386 ``sender`` will be the ``foo.bar.myapp.models`` module.
392 A list of the model classes from any app which :djadmin:`syncdb` has
396 Indicates how much information manage.py is printing on screen. See
397 the :djadminopt:`--verbosity` flag for details.
399 Functions which listen for :data:`post_syncdb` should adjust what they
400 output to the screen based on the value of this argument.
403 If ``interactive`` is ``True``, it's safe to prompt the user to input
404 things on the command line. If ``interactive`` is ``False``, functions
405 which listen for this signal should not try to prompt for anything.
407 For example, the :mod:`django.contrib.auth` app only prompts to create a
408 superuser when ``interactive`` is ``True``.
410 For example, ``yourapp/management/__init__.py`` could be written like::
412 from django.db.models.signals import post_syncdb
413 import yourapp.models
415 def my_callback(sender, **kwargs):
416 # Your specific logic here
419 post_syncdb.connect(my_callback, sender=yourapp.models)
421 Request/response signals
422 ========================
424 .. module:: django.core.signals
425 :synopsis: Core signals sent by the request/response system.
427 Signals sent by the core framework when processing a request.
432 .. data:: django.core.signals.request_started
435 Sent when Django begins processing an HTTP request.
437 Arguments sent with this signal:
440 The handler class -- e.g.
441 :class:`django.core.handlers.wsgi.WsgiHandler` -- that handled
447 .. data:: django.core.signals.request_finished
450 Sent when Django finishes processing an HTTP request.
452 Arguments sent with this signal:
455 The handler class, as above.
457 got_request_exception
458 ---------------------
460 .. data:: django.core.signals.got_request_exception
463 This signal is sent whenever Django encounters an exception while processing an incoming HTTP request.
465 Arguments sent with this signal:
468 The handler class, as above.
471 The :class:`~django.http.HttpRequest` object.
476 .. module:: django.test.signals
477 :synopsis: Signals sent during testing.
479 Signals only sent when :doc:`running tests </topics/testing>`.
484 .. versionadded:: 1.4
486 .. data:: django.test.signals.setting_changed
489 This signal is sent when the value of a setting is changed through the
490 :meth:`django.test.TestCase.setting` context manager or the
491 :func:`django.test.utils.override_settings` decorator/context manager.
493 It's actually sent twice: when the new value is applied ("setup") and when the
494 original value is restored ("teardown").
496 Arguments sent with this signal:
499 The settings handler.
502 The name of the setting.
505 The value of the setting after the change. For settings that initially
506 don't exist, in the "teardown" phase, ``value`` is ``None``.
511 .. data:: django.test.signals.template_rendered
514 Sent when the test system renders a template. This signal is not emitted during
515 normal operation of a Django server -- it is only available during testing.
517 Arguments sent with this signal:
520 The :class:`~django.template.Template` object which was rendered.
526 The :class:`~django.template.Context` with which the template was
532 .. module:: django.db.backends
533 :synopsis: Core signals sent by the database wrapper.
535 Signals sent by the database wrapper when a database connection is
541 .. data:: django.db.backends.signals.connection_created
544 .. versionchanged:: 1.2
545 The connection argument was added
547 Sent when the database wrapper makes the initial connection to the
548 database. This is particularly useful if you'd like to send any post
549 connection commands to the SQL backend.
551 Arguments sent with this signal:
554 The database wrapper class -- i.e.
555 :class:`django.db.backends.postgresql_psycopg2.DatabaseWrapper` or
556 :class:`django.db.backends.mysql.DatabaseWrapper`, etc.
559 The database connection that was opened. This can be used in a
560 multiple-database configuration to differentiate connection signals
561 from different databases.