2009-12-26 11:44:21 +08:00
|
|
|
=======
|
|
|
|
Signals
|
|
|
|
=======
|
2008-08-27 03:04:52 +08:00
|
|
|
|
2016-04-13 16:04:24 +08:00
|
|
|
A list of all the signals that Django sends. All built-in signals are sent
|
|
|
|
using the :meth:`~django.dispatch.Signal.send` method.
|
2008-08-27 03:04:52 +08:00
|
|
|
|
|
|
|
.. seealso::
|
|
|
|
|
2010-08-20 03:27:44 +08:00
|
|
|
See the documentation on the :doc:`signal dispatcher </topics/signals>` for
|
2009-09-13 14:18:16 +08:00
|
|
|
information regarding how to register for and receive signals.
|
2009-09-13 07:06:22 +08:00
|
|
|
|
2012-12-29 03:00:11 +08:00
|
|
|
The :doc:`authentication framework </topics/auth/index>` sends :ref:`signals when
|
2010-11-26 21:33:27 +08:00
|
|
|
a user is logged in / out <topics-auth-signals>`.
|
|
|
|
|
2008-08-27 03:04:52 +08:00
|
|
|
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
|
2013-11-05 12:11:51 +08:00
|
|
|
model system.
|
2008-08-27 03:04:52 +08:00
|
|
|
|
|
|
|
.. warning::
|
|
|
|
|
|
|
|
Many of these signals are sent by various model methods like
|
2012-12-29 23:35:12 +08:00
|
|
|
``__init__()`` or :meth:`~django.db.models.Model.save` that you can
|
|
|
|
override in your own code.
|
2008-08-27 03:04:52 +08:00
|
|
|
|
|
|
|
If you override these methods on your model, you must call the parent class'
|
2019-10-30 23:52:40 +08:00
|
|
|
methods for these signals to be sent.
|
2008-08-27 03:04:52 +08:00
|
|
|
|
2010-08-07 14:27:52 +08:00
|
|
|
Note also that Django stores signal handlers as weak references by default,
|
|
|
|
so if your handler is a local function, it may be garbage collected. To
|
|
|
|
prevent this, pass ``weak=False`` when you call the signal's :meth:`~django.dispatch.Signal.connect`.
|
2009-04-01 07:34:03 +08:00
|
|
|
|
2015-01-27 04:39:52 +08:00
|
|
|
.. note::
|
2013-11-05 12:11:51 +08:00
|
|
|
|
|
|
|
Model signals ``sender`` model can be lazily referenced when connecting a
|
|
|
|
receiver by specifying its full application label. For example, an
|
2019-06-19 02:38:21 +08:00
|
|
|
``Question`` model defined in the ``polls`` application could be referenced
|
|
|
|
as ``'polls.Question'``. This sort of reference can be quite handy when
|
2013-11-05 12:11:51 +08:00
|
|
|
dealing with circular import dependencies and swappable models.
|
|
|
|
|
2016-01-25 05:26:11 +08:00
|
|
|
``pre_init``
|
|
|
|
------------
|
2008-08-27 03:04:52 +08:00
|
|
|
|
2008-08-27 03:23:59 +08:00
|
|
|
.. attribute:: django.db.models.signals.pre_init
|
|
|
|
:module:
|
|
|
|
|
|
|
|
.. ^^^^^^^ this :module: hack keeps Sphinx from prepending the module.
|
2008-08-27 03:04:52 +08:00
|
|
|
|
2012-09-22 07:22:50 +08:00
|
|
|
Whenever you instantiate a Django model, this signal is sent at the beginning
|
2012-12-29 23:35:12 +08:00
|
|
|
of the model's ``__init__()`` method.
|
2008-08-27 03:04:52 +08:00
|
|
|
|
|
|
|
Arguments sent with this signal:
|
|
|
|
|
2011-02-08 06:18:14 +08:00
|
|
|
``sender``
|
|
|
|
The model class that just had an instance created.
|
2008-08-27 03:04:52 +08:00
|
|
|
|
2011-02-08 06:18:14 +08:00
|
|
|
``args``
|
2019-06-18 20:19:19 +08:00
|
|
|
A list of positional arguments passed to ``__init__()``.
|
2008-08-27 03:04:52 +08:00
|
|
|
|
2011-02-08 06:18:14 +08:00
|
|
|
``kwargs``
|
2019-06-18 20:19:19 +08:00
|
|
|
A dictionary of keyword arguments passed to ``__init__()``.
|
2008-08-27 03:04:52 +08:00
|
|
|
|
2019-06-18 20:19:19 +08:00
|
|
|
For example, the :doc:`tutorial </intro/tutorial02>` has this line::
|
2008-08-27 03:04:52 +08:00
|
|
|
|
2019-06-18 20:19:19 +08:00
|
|
|
q = Question(question_text="What's new?", pub_date=timezone.now())
|
2008-08-27 03:04:52 +08:00
|
|
|
|
|
|
|
The arguments sent to a :data:`pre_init` handler would be:
|
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
========== ===============================================================
|
|
|
|
Argument Value
|
|
|
|
========== ===============================================================
|
2019-06-18 20:19:19 +08:00
|
|
|
``sender`` ``Question`` (the class itself)
|
2008-08-27 03:04:52 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
``args`` ``[]`` (an empty list because there were no positional
|
2019-06-18 20:19:19 +08:00
|
|
|
arguments passed to ``__init__()``)
|
2008-08-27 03:04:52 +08:00
|
|
|
|
2019-06-18 20:19:19 +08:00
|
|
|
``kwargs`` ``{'question_text': "What's new?",``
|
|
|
|
``'pub_date': datetime.datetime(2012, 2, 26, 13, 0, 0, 775217, tzinfo=<UTC>)}``
|
2011-10-14 08:12:01 +08:00
|
|
|
========== ===============================================================
|
2008-08-27 03:04:52 +08:00
|
|
|
|
2016-01-25 05:26:11 +08:00
|
|
|
``post_init``
|
|
|
|
-------------
|
2008-08-27 03:04:52 +08:00
|
|
|
|
|
|
|
.. data:: django.db.models.signals.post_init
|
2008-08-27 03:23:59 +08:00
|
|
|
:module:
|
2008-08-27 03:04:52 +08:00
|
|
|
|
2012-12-29 23:35:12 +08:00
|
|
|
Like pre_init, but this one is sent when the ``__init__()`` method finishes.
|
2008-08-27 03:04:52 +08:00
|
|
|
|
|
|
|
Arguments sent with this signal:
|
|
|
|
|
2011-02-08 06:18:14 +08:00
|
|
|
``sender``
|
|
|
|
As above: the model class that just had an instance created.
|
2008-08-27 03:04:52 +08:00
|
|
|
|
2011-02-08 06:18:14 +08:00
|
|
|
``instance``
|
|
|
|
The actual instance of the model that's just been created.
|
2008-08-27 03:04:52 +08:00
|
|
|
|
2019-07-19 19:02:57 +08:00
|
|
|
.. note::
|
|
|
|
|
|
|
|
``instance._state`` isn't set before sending the ``post_init`` signal,
|
|
|
|
so ``_state`` attributes always have their default values. For example,
|
|
|
|
``_state.db`` is ``None`` and cannot be used to check an ``instance``
|
|
|
|
database.
|
|
|
|
|
2019-07-19 19:05:31 +08:00
|
|
|
.. warning::
|
|
|
|
|
|
|
|
For performance reasons, you shouldn't perform queries in receivers of
|
|
|
|
``pre_init`` or ``post_init`` signals because they would be executed for
|
|
|
|
each instance returned during queryset iteration.
|
|
|
|
|
2016-01-25 05:26:11 +08:00
|
|
|
``pre_save``
|
|
|
|
------------
|
2008-08-27 03:04:52 +08:00
|
|
|
|
|
|
|
.. data:: django.db.models.signals.pre_save
|
2008-08-27 03:23:59 +08:00
|
|
|
:module:
|
2009-09-13 07:06:22 +08:00
|
|
|
|
2008-08-27 03:04:52 +08:00
|
|
|
This is sent at the beginning of a model's :meth:`~django.db.models.Model.save`
|
|
|
|
method.
|
|
|
|
|
|
|
|
Arguments sent with this signal:
|
|
|
|
|
2011-02-08 06:18:14 +08:00
|
|
|
``sender``
|
|
|
|
The model class.
|
2008-08-27 03:04:52 +08:00
|
|
|
|
2011-02-08 06:18:14 +08:00
|
|
|
``instance``
|
|
|
|
The actual instance being saved.
|
2008-08-27 03:04:52 +08:00
|
|
|
|
2011-06-17 23:39:42 +08:00
|
|
|
``raw``
|
|
|
|
A boolean; ``True`` if the model is saved exactly as presented
|
|
|
|
(i.e. when loading a fixture). One should not query/modify other
|
|
|
|
records in the database as the database might not be in a
|
|
|
|
consistent state yet.
|
|
|
|
|
2011-02-08 06:18:14 +08:00
|
|
|
``using``
|
|
|
|
The database alias being used.
|
2010-08-07 14:27:52 +08:00
|
|
|
|
2012-05-12 15:24:20 +08:00
|
|
|
``update_fields``
|
2016-07-21 22:06:41 +08:00
|
|
|
The set of fields to update as passed to :meth:`.Model.save`, or ``None``
|
|
|
|
if ``update_fields`` wasn't passed to ``save()``.
|
2012-05-12 15:24:20 +08:00
|
|
|
|
2016-01-25 05:26:11 +08:00
|
|
|
``post_save``
|
|
|
|
-------------
|
2008-08-27 03:04:52 +08:00
|
|
|
|
|
|
|
.. data:: django.db.models.signals.post_save
|
2009-09-13 07:06:22 +08:00
|
|
|
:module:
|
|
|
|
|
2008-08-27 03:04:52 +08:00
|
|
|
Like :data:`pre_save`, but sent at the end of the
|
|
|
|
:meth:`~django.db.models.Model.save` method.
|
|
|
|
|
|
|
|
Arguments sent with this signal:
|
|
|
|
|
2011-02-08 06:18:14 +08:00
|
|
|
``sender``
|
|
|
|
The model class.
|
2008-08-27 03:04:52 +08:00
|
|
|
|
2011-02-08 06:18:14 +08:00
|
|
|
``instance``
|
|
|
|
The actual instance being saved.
|
2008-08-27 03:04:52 +08:00
|
|
|
|
2011-02-08 06:18:14 +08:00
|
|
|
``created``
|
|
|
|
A boolean; ``True`` if a new record was created.
|
2008-08-27 03:04:52 +08:00
|
|
|
|
2011-06-17 23:39:42 +08:00
|
|
|
``raw``
|
|
|
|
A boolean; ``True`` if the model is saved exactly as presented
|
|
|
|
(i.e. when loading a fixture). One should not query/modify other
|
|
|
|
records in the database as the database might not be in a
|
|
|
|
consistent state yet.
|
|
|
|
|
2011-02-08 06:18:14 +08:00
|
|
|
``using``
|
|
|
|
The database alias being used.
|
2010-08-07 14:27:52 +08:00
|
|
|
|
2012-05-12 15:24:20 +08:00
|
|
|
``update_fields``
|
2016-07-21 22:06:41 +08:00
|
|
|
The set of fields to update as passed to :meth:`.Model.save`, or ``None``
|
|
|
|
if ``update_fields`` wasn't passed to ``save()``.
|
2012-05-12 15:24:20 +08:00
|
|
|
|
2016-01-25 05:26:11 +08:00
|
|
|
``pre_delete``
|
|
|
|
--------------
|
2008-08-27 03:04:52 +08:00
|
|
|
|
|
|
|
.. data:: django.db.models.signals.pre_delete
|
2008-08-27 03:23:59 +08:00
|
|
|
:module:
|
2009-09-13 07:06:22 +08:00
|
|
|
|
2008-08-27 03:04:52 +08:00
|
|
|
Sent at the beginning of a model's :meth:`~django.db.models.Model.delete`
|
2011-04-06 18:12:21 +08:00
|
|
|
method and a queryset's :meth:`~django.db.models.query.QuerySet.delete` method.
|
2008-08-27 03:04:52 +08:00
|
|
|
|
|
|
|
Arguments sent with this signal:
|
|
|
|
|
2011-02-08 06:18:14 +08:00
|
|
|
``sender``
|
|
|
|
The model class.
|
|
|
|
|
|
|
|
``instance``
|
|
|
|
The actual instance being deleted.
|
2008-08-27 03:04:52 +08:00
|
|
|
|
2011-02-08 06:18:14 +08:00
|
|
|
``using``
|
|
|
|
The database alias being used.
|
2010-08-07 14:27:52 +08:00
|
|
|
|
2016-01-25 05:26:11 +08:00
|
|
|
``post_delete``
|
|
|
|
---------------
|
2008-08-27 03:04:52 +08:00
|
|
|
|
|
|
|
.. data:: django.db.models.signals.post_delete
|
2009-09-13 07:06:22 +08:00
|
|
|
:module:
|
|
|
|
|
2011-04-06 18:12:21 +08:00
|
|
|
Like :data:`pre_delete`, but sent at the end of a model's
|
|
|
|
:meth:`~django.db.models.Model.delete` method and a queryset's
|
|
|
|
:meth:`~django.db.models.query.QuerySet.delete` method.
|
2008-08-27 03:04:52 +08:00
|
|
|
|
|
|
|
Arguments sent with this signal:
|
|
|
|
|
2011-02-08 06:18:14 +08:00
|
|
|
``sender``
|
|
|
|
The model class.
|
|
|
|
|
|
|
|
``instance``
|
|
|
|
The actual instance being deleted.
|
2008-08-27 03:04:52 +08:00
|
|
|
|
2011-02-08 06:18:14 +08:00
|
|
|
Note that the object will no longer be in the database, so be very
|
|
|
|
careful what you do with this instance.
|
2008-08-27 03:04:52 +08:00
|
|
|
|
2011-02-08 06:18:14 +08:00
|
|
|
``using``
|
|
|
|
The database alias being used.
|
2010-08-07 14:27:52 +08:00
|
|
|
|
2016-01-25 05:26:11 +08:00
|
|
|
``m2m_changed``
|
|
|
|
---------------
|
2010-01-13 19:07:16 +08:00
|
|
|
|
|
|
|
.. data:: django.db.models.signals.m2m_changed
|
|
|
|
:module:
|
|
|
|
|
2012-12-25 22:56:22 +08:00
|
|
|
Sent when a :class:`~django.db.models.ManyToManyField` is changed on a model
|
|
|
|
instance. Strictly speaking, this is not a model signal since it is sent by the
|
|
|
|
:class:`~django.db.models.ManyToManyField`, but since it complements the
|
2010-01-13 19:07:16 +08:00
|
|
|
:data:`pre_save`/:data:`post_save` and :data:`pre_delete`/:data:`post_delete`
|
|
|
|
when it comes to tracking changes to models, it is included here.
|
|
|
|
|
|
|
|
Arguments sent with this signal:
|
|
|
|
|
2011-02-08 06:18:14 +08:00
|
|
|
``sender``
|
2012-12-25 22:56:22 +08:00
|
|
|
The intermediate model class describing the
|
|
|
|
:class:`~django.db.models.ManyToManyField`. This class is automatically
|
|
|
|
created when a many-to-many field is defined; you can access it using the
|
|
|
|
``through`` attribute on the many-to-many field.
|
2011-02-08 06:18:14 +08:00
|
|
|
|
|
|
|
``instance``
|
|
|
|
The instance whose many-to-many relation is updated. This can be an
|
2012-12-25 22:56:22 +08:00
|
|
|
instance of the ``sender``, or of the class the
|
|
|
|
:class:`~django.db.models.ManyToManyField` is related to.
|
2011-02-08 06:18:14 +08:00
|
|
|
|
|
|
|
``action``
|
|
|
|
A string indicating the type of update that is done on the relation.
|
|
|
|
This can be one of the following:
|
|
|
|
|
|
|
|
``"pre_add"``
|
2011-04-25 07:47:18 +08:00
|
|
|
Sent *before* one or more objects are added to the relation.
|
2011-02-08 06:18:14 +08:00
|
|
|
``"post_add"``
|
2011-04-25 07:47:18 +08:00
|
|
|
Sent *after* one or more objects are added to the relation.
|
2011-02-08 06:18:14 +08:00
|
|
|
``"pre_remove"``
|
2011-04-25 07:47:18 +08:00
|
|
|
Sent *before* one or more objects are removed from the relation.
|
2011-02-08 06:18:14 +08:00
|
|
|
``"post_remove"``
|
2011-04-25 07:47:18 +08:00
|
|
|
Sent *after* one or more objects are removed from the relation.
|
2011-02-08 06:18:14 +08:00
|
|
|
``"pre_clear"``
|
2011-04-25 07:47:18 +08:00
|
|
|
Sent *before* the relation is cleared.
|
2011-02-08 06:18:14 +08:00
|
|
|
``"post_clear"``
|
2011-04-25 07:47:18 +08:00
|
|
|
Sent *after* the relation is cleared.
|
2011-02-08 06:18:14 +08:00
|
|
|
|
|
|
|
``reverse``
|
|
|
|
Indicates which side of the relation is updated (i.e., if it is the
|
|
|
|
forward or reverse relation that is being modified).
|
|
|
|
|
|
|
|
``model``
|
|
|
|
The class of the objects that are added to, removed from or cleared
|
|
|
|
from the relation.
|
|
|
|
|
|
|
|
``pk_set``
|
2019-09-18 22:08:56 +08:00
|
|
|
For the ``pre_add`` and ``post_add`` actions, this is a set of primary key
|
|
|
|
values that will be, or have been, added to the relation. This may be a
|
|
|
|
subset of the values submitted to be added, since inserts must filter
|
|
|
|
existing values in order to avoid a database ``IntegrityError``.
|
|
|
|
|
|
|
|
For the ``pre_remove`` and ``post_remove`` actions, this is a set of
|
|
|
|
primary key values that was submitted to be removed from the relation. This
|
|
|
|
is not dependent on whether the values actually will be, or have been,
|
|
|
|
removed. In particular, non-existent values may be submitted, and will
|
|
|
|
appear in ``pk_set``, even though they have no effect on the database.
|
2011-02-08 06:18:14 +08:00
|
|
|
|
|
|
|
For the ``pre_clear`` and ``post_clear`` actions, this is ``None``.
|
|
|
|
|
|
|
|
``using``
|
|
|
|
The database alias being used.
|
2010-08-07 14:27:52 +08:00
|
|
|
|
2010-01-13 19:07:16 +08:00
|
|
|
For example, if a ``Pizza`` can have multiple ``Topping`` objects, modeled
|
2011-10-14 08:12:01 +08:00
|
|
|
like this::
|
2010-01-13 19:07:16 +08:00
|
|
|
|
|
|
|
class Topping(models.Model):
|
|
|
|
# ...
|
2011-10-14 08:12:01 +08:00
|
|
|
pass
|
2010-01-13 19:07:16 +08:00
|
|
|
|
|
|
|
class Pizza(models.Model):
|
|
|
|
# ...
|
|
|
|
toppings = models.ManyToManyField(Topping)
|
|
|
|
|
2012-09-19 19:13:10 +08:00
|
|
|
If we connected a handler like this::
|
|
|
|
|
2014-09-02 13:59:05 +08:00
|
|
|
from django.db.models.signals import m2m_changed
|
|
|
|
|
2012-09-19 19:13:10 +08:00
|
|
|
def toppings_changed(sender, **kwargs):
|
|
|
|
# Do something
|
|
|
|
pass
|
|
|
|
|
|
|
|
m2m_changed.connect(toppings_changed, sender=Pizza.toppings.through)
|
|
|
|
|
|
|
|
and then did something like this::
|
2010-01-13 19:07:16 +08:00
|
|
|
|
2013-06-28 00:39:50 +08:00
|
|
|
>>> p = Pizza.objects.create(...)
|
2010-01-13 19:07:16 +08:00
|
|
|
>>> t = Topping.objects.create(...)
|
|
|
|
>>> p.toppings.add(t)
|
|
|
|
|
2013-09-24 06:36:33 +08:00
|
|
|
the arguments sent to a :data:`m2m_changed` handler (``toppings_changed`` in
|
2012-09-19 19:13:10 +08:00
|
|
|
the example above) would be:
|
2010-01-13 19:07:16 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
============== ============================================================
|
|
|
|
Argument Value
|
|
|
|
============== ============================================================
|
|
|
|
``sender`` ``Pizza.toppings.through`` (the intermediate m2m class)
|
2010-01-13 19:07:16 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
``instance`` ``p`` (the ``Pizza`` instance being modified)
|
2010-01-13 19:07:16 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
``action`` ``"pre_add"`` (followed by a separate signal with ``"post_add"``)
|
2010-01-13 19:07:16 +08:00
|
|
|
|
2012-12-25 22:56:22 +08:00
|
|
|
``reverse`` ``False`` (``Pizza`` contains the
|
|
|
|
:class:`~django.db.models.ManyToManyField`, so this call
|
|
|
|
modifies the forward relation)
|
2010-01-13 19:07:16 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
``model`` ``Topping`` (the class of the objects added to the
|
|
|
|
``Pizza``)
|
2010-08-07 14:27:52 +08:00
|
|
|
|
2014-09-26 20:31:50 +08:00
|
|
|
``pk_set`` ``{t.id}`` (since only ``Topping t`` was added to the relation)
|
2010-01-13 19:07:16 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
``using`` ``"default"`` (since the default router sends writes here)
|
|
|
|
============== ============================================================
|
2010-01-13 19:07:16 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
And if we would then do something like this::
|
2010-01-13 19:07:16 +08:00
|
|
|
|
|
|
|
>>> t.pizza_set.remove(p)
|
|
|
|
|
|
|
|
the arguments sent to a :data:`m2m_changed` handler would be:
|
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
============== ============================================================
|
|
|
|
Argument Value
|
|
|
|
============== ============================================================
|
|
|
|
``sender`` ``Pizza.toppings.through`` (the intermediate m2m class)
|
2010-01-13 19:07:16 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
``instance`` ``t`` (the ``Topping`` instance being modified)
|
2010-01-13 19:07:16 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
``action`` ``"pre_remove"`` (followed by a separate signal with ``"post_remove"``)
|
2010-01-13 19:07:16 +08:00
|
|
|
|
2012-12-25 22:56:22 +08:00
|
|
|
``reverse`` ``True`` (``Pizza`` contains the
|
|
|
|
:class:`~django.db.models.ManyToManyField`, so this call
|
|
|
|
modifies the reverse relation)
|
2010-01-13 19:07:16 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
``model`` ``Pizza`` (the class of the objects removed from the
|
|
|
|
``Topping``)
|
2010-01-13 19:07:16 +08:00
|
|
|
|
2014-09-26 20:31:50 +08:00
|
|
|
``pk_set`` ``{p.id}`` (since only ``Pizza p`` was removed from the
|
2011-10-14 08:12:01 +08:00
|
|
|
relation)
|
2010-08-07 14:27:52 +08:00
|
|
|
|
2011-10-14 08:12:01 +08:00
|
|
|
``using`` ``"default"`` (since the default router sends writes here)
|
|
|
|
============== ============================================================
|
2010-01-13 19:07:16 +08:00
|
|
|
|
2016-01-25 05:26:11 +08:00
|
|
|
``class_prepared``
|
|
|
|
------------------
|
2008-08-27 03:04:52 +08:00
|
|
|
|
|
|
|
.. data:: django.db.models.signals.class_prepared
|
2008-08-27 03:23:59 +08:00
|
|
|
:module:
|
2009-09-13 07:06:22 +08:00
|
|
|
|
2008-08-27 03:04:52 +08:00
|
|
|
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.
|
|
|
|
|
2013-12-30 20:24:24 +08:00
|
|
|
Since this signal is sent during the app registry population process, and
|
2014-01-01 00:55:12 +08:00
|
|
|
:meth:`AppConfig.ready() <django.apps.AppConfig.ready>` runs after the app
|
2013-12-30 20:24:24 +08:00
|
|
|
registry is fully populated, receivers cannot be connected in that method.
|
|
|
|
One possibility is to connect them ``AppConfig.__init__()`` instead, taking
|
|
|
|
care not to import models or trigger calls to the app registry.
|
|
|
|
|
2008-08-27 03:04:52 +08:00
|
|
|
Arguments that are sent with this signal:
|
|
|
|
|
|
|
|
``sender``
|
|
|
|
The model class which was just prepared.
|
|
|
|
|
|
|
|
Management signals
|
|
|
|
==================
|
|
|
|
|
2010-08-20 03:27:44 +08:00
|
|
|
Signals sent by :doc:`django-admin </ref/django-admin>`.
|
2008-08-27 03:04:52 +08:00
|
|
|
|
2016-01-25 05:26:11 +08:00
|
|
|
``pre_migrate``
|
|
|
|
---------------
|
2013-05-18 06:18:35 +08:00
|
|
|
|
2013-07-30 18:52:52 +08:00
|
|
|
.. data:: django.db.models.signals.pre_migrate
|
2013-05-18 06:18:35 +08:00
|
|
|
:module:
|
|
|
|
|
2013-07-30 18:52:52 +08:00
|
|
|
Sent by the :djadmin:`migrate` command before it starts to install an
|
2014-02-28 22:22:33 +08:00
|
|
|
application. It's not emitted for applications that lack a ``models`` module.
|
2013-05-18 06:18:35 +08:00
|
|
|
|
|
|
|
Arguments sent with this signal:
|
|
|
|
|
|
|
|
``sender``
|
2013-12-28 03:55:58 +08:00
|
|
|
An :class:`~django.apps.AppConfig` instance for the application about to
|
|
|
|
be migrated/synced.
|
2013-05-18 06:18:35 +08:00
|
|
|
|
2013-12-28 03:55:58 +08:00
|
|
|
``app_config``
|
2013-05-18 06:18:35 +08:00
|
|
|
Same as ``sender``.
|
|
|
|
|
|
|
|
``verbosity``
|
|
|
|
Indicates how much information manage.py is printing on screen. See
|
2016-01-12 09:59:34 +08:00
|
|
|
the :option:`--verbosity` flag for details.
|
2013-05-18 06:18:35 +08:00
|
|
|
|
2013-07-30 18:52:52 +08:00
|
|
|
Functions which listen for :data:`pre_migrate` should adjust what they
|
2013-05-18 06:18:35 +08:00
|
|
|
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``.
|
|
|
|
|
2014-01-13 05:20:53 +08:00
|
|
|
``using``
|
2013-05-18 06:18:35 +08:00
|
|
|
The alias of database on which a command will operate.
|
|
|
|
|
2016-05-13 23:58:54 +08:00
|
|
|
``plan``
|
|
|
|
The migration plan that is going to be used for the migration run. While
|
|
|
|
the plan is not public API, this allows for the rare cases when it is
|
|
|
|
necessary to know the plan. A plan is a list of two-tuples with the first
|
|
|
|
item being the instance of a migration class and the second item showing
|
|
|
|
if the migration was rolled back (``True``) or applied (``False``).
|
|
|
|
|
|
|
|
``apps``
|
|
|
|
An instance of :data:`Apps <django.apps>` containing the state of the
|
|
|
|
project before the migration run. It should be used instead of the global
|
|
|
|
:attr:`apps <django.apps.apps>` registry to retrieve the models you
|
|
|
|
want to perform operations on.
|
|
|
|
|
2016-01-25 05:26:11 +08:00
|
|
|
``post_migrate``
|
|
|
|
----------------
|
2013-07-30 18:52:52 +08:00
|
|
|
|
|
|
|
.. data:: django.db.models.signals.post_migrate
|
|
|
|
:module:
|
|
|
|
|
2016-10-19 17:37:04 +08:00
|
|
|
Sent at the end of the :djadmin:`migrate` (even if no migrations are run) and
|
|
|
|
:djadmin:`flush` commands. It's not emitted for applications that lack a
|
2014-02-28 22:22:33 +08:00
|
|
|
``models`` module.
|
2008-08-27 03:04:52 +08:00
|
|
|
|
2016-10-19 17:37:04 +08:00
|
|
|
Handlers of this signal must not perform database schema alterations as doing
|
|
|
|
so may cause the :djadmin:`flush` command to fail if it runs during the
|
|
|
|
:djadmin:`migrate` command.
|
2008-08-27 03:04:52 +08:00
|
|
|
|
|
|
|
Arguments sent with this signal:
|
|
|
|
|
2011-02-08 06:18:14 +08:00
|
|
|
``sender``
|
2013-12-28 03:55:58 +08:00
|
|
|
An :class:`~django.apps.AppConfig` instance for the application that was
|
|
|
|
just installed.
|
2008-08-27 03:04:52 +08:00
|
|
|
|
2013-12-28 03:55:58 +08:00
|
|
|
``app_config``
|
2011-02-08 06:18:14 +08:00
|
|
|
Same as ``sender``.
|
2008-08-27 03:04:52 +08:00
|
|
|
|
2011-02-08 06:18:14 +08:00
|
|
|
``verbosity``
|
|
|
|
Indicates how much information manage.py is printing on screen. See
|
2016-01-12 09:59:34 +08:00
|
|
|
the :option:`--verbosity` flag for details.
|
2008-08-27 03:04:52 +08:00
|
|
|
|
2013-07-30 18:52:52 +08:00
|
|
|
Functions which listen for :data:`post_migrate` should adjust what they
|
2011-02-08 06:18:14 +08:00
|
|
|
output to the screen based on the value of this argument.
|
2008-08-27 03:04:52 +08:00
|
|
|
|
2011-02-08 06:18:14 +08:00
|
|
|
``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.
|
2008-08-27 03:04:52 +08:00
|
|
|
|
2011-02-08 06:18:14 +08:00
|
|
|
For example, the :mod:`django.contrib.auth` app only prompts to create a
|
|
|
|
superuser when ``interactive`` is ``True``.
|
2008-08-27 03:04:52 +08:00
|
|
|
|
2014-08-29 03:30:20 +08:00
|
|
|
``using``
|
2012-12-31 06:59:02 +08:00
|
|
|
The database alias used for synchronization. Defaults to the ``default``
|
|
|
|
database.
|
|
|
|
|
2016-05-13 23:58:54 +08:00
|
|
|
``plan``
|
|
|
|
The migration plan that was used for the migration run. While the plan is
|
|
|
|
not public API, this allows for the rare cases when it is necessary to
|
|
|
|
know the plan. A plan is a list of two-tuples with the first item being
|
|
|
|
the instance of a migration class and the second item showing if the
|
|
|
|
migration was rolled back (``True``) or applied (``False``).
|
|
|
|
|
|
|
|
``apps``
|
|
|
|
An instance of :data:`Apps <django.apps.apps>` containing the state of the
|
|
|
|
project after the migration run. It should be used instead of the global
|
|
|
|
:attr:`apps <django.apps.apps>` registry to retrieve the models you
|
|
|
|
want to perform operations on.
|
|
|
|
|
2014-06-02 23:35:20 +08:00
|
|
|
For example, you could register a callback in an
|
|
|
|
:class:`~django.apps.AppConfig` like this::
|
2010-12-29 23:47:46 +08:00
|
|
|
|
2014-06-02 23:35:20 +08:00
|
|
|
from django.apps import AppConfig
|
2013-07-30 18:52:52 +08:00
|
|
|
from django.db.models.signals import post_migrate
|
2010-12-29 23:47:46 +08:00
|
|
|
|
|
|
|
def my_callback(sender, **kwargs):
|
|
|
|
# Your specific logic here
|
|
|
|
pass
|
|
|
|
|
2014-06-02 23:35:20 +08:00
|
|
|
class MyAppConfig(AppConfig):
|
|
|
|
...
|
|
|
|
|
|
|
|
def ready(self):
|
|
|
|
post_migrate.connect(my_callback, sender=self)
|
2013-07-30 18:52:52 +08:00
|
|
|
|
2014-11-28 00:22:23 +08:00
|
|
|
.. note::
|
|
|
|
|
|
|
|
If you provide an :class:`~django.apps.AppConfig` instance as the sender
|
|
|
|
argument, please ensure that the signal is registered in
|
|
|
|
:meth:`~django.apps.AppConfig.ready`. ``AppConfig``\s are recreated for
|
|
|
|
tests that run with a modified set of :setting:`INSTALLED_APPS` (such as
|
|
|
|
when settings are overridden) and such signals should be connected for each
|
|
|
|
new ``AppConfig`` instance.
|
|
|
|
|
2008-08-27 03:04:52 +08:00
|
|
|
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.
|
|
|
|
|
2016-01-25 05:26:11 +08:00
|
|
|
``request_started``
|
|
|
|
-------------------
|
2008-08-27 03:04:52 +08:00
|
|
|
|
|
|
|
.. data:: django.core.signals.request_started
|
2009-09-13 07:06:22 +08:00
|
|
|
:module:
|
|
|
|
|
2008-08-27 03:04:52 +08:00
|
|
|
Sent when Django begins processing an HTTP request.
|
|
|
|
|
|
|
|
Arguments sent with this signal:
|
|
|
|
|
2011-02-08 06:18:14 +08:00
|
|
|
``sender``
|
2013-01-01 21:12:42 +08:00
|
|
|
The handler class -- e.g. ``django.core.handlers.wsgi.WsgiHandler`` -- that
|
|
|
|
handled the request.
|
2013-11-21 09:03:02 +08:00
|
|
|
``environ``
|
|
|
|
The ``environ`` dictionary provided to the request.
|
|
|
|
|
2016-01-25 05:26:11 +08:00
|
|
|
``request_finished``
|
|
|
|
--------------------
|
2008-08-27 03:04:52 +08:00
|
|
|
|
|
|
|
.. data:: django.core.signals.request_finished
|
2008-08-27 03:23:59 +08:00
|
|
|
:module:
|
2009-09-13 07:06:22 +08:00
|
|
|
|
2014-02-23 01:30:28 +08:00
|
|
|
Sent when Django finishes delivering an HTTP response to the client.
|
2012-12-30 22:19:22 +08:00
|
|
|
|
2008-08-27 03:04:52 +08:00
|
|
|
Arguments sent with this signal:
|
|
|
|
|
2011-02-08 06:18:14 +08:00
|
|
|
``sender``
|
|
|
|
The handler class, as above.
|
2008-08-27 03:04:52 +08:00
|
|
|
|
2016-01-25 05:26:11 +08:00
|
|
|
``got_request_exception``
|
|
|
|
-------------------------
|
2008-08-27 03:04:52 +08:00
|
|
|
|
|
|
|
.. data:: django.core.signals.got_request_exception
|
2008-08-27 03:23:59 +08:00
|
|
|
:module:
|
2009-09-13 07:06:22 +08:00
|
|
|
|
2008-08-27 03:04:52 +08:00
|
|
|
This signal is sent whenever Django encounters an exception while processing an incoming HTTP request.
|
|
|
|
|
|
|
|
Arguments sent with this signal:
|
|
|
|
|
2011-02-08 06:18:14 +08:00
|
|
|
``sender``
|
2018-11-20 23:09:45 +08:00
|
|
|
Unused (always ``None``).
|
2008-08-27 03:04:52 +08:00
|
|
|
|
2011-02-08 06:18:14 +08:00
|
|
|
``request``
|
|
|
|
The :class:`~django.http.HttpRequest` object.
|
2008-08-27 03:04:52 +08:00
|
|
|
|
|
|
|
Test signals
|
|
|
|
============
|
|
|
|
|
|
|
|
.. module:: django.test.signals
|
|
|
|
:synopsis: Signals sent during testing.
|
|
|
|
|
2012-12-22 08:59:06 +08:00
|
|
|
Signals only sent when :ref:`running tests <running-tests>`.
|
2008-08-27 03:04:52 +08:00
|
|
|
|
2016-01-25 05:26:11 +08:00
|
|
|
``setting_changed``
|
|
|
|
-------------------
|
2011-05-18 20:08:53 +08:00
|
|
|
|
|
|
|
.. data:: django.test.signals.setting_changed
|
|
|
|
:module:
|
|
|
|
|
2012-03-14 07:02:31 +08:00
|
|
|
This signal is sent when the value of a setting is changed through the
|
2013-01-01 21:12:42 +08:00
|
|
|
``django.test.TestCase.settings()`` context manager or the
|
2013-09-09 16:59:47 +08:00
|
|
|
:func:`django.test.override_settings` decorator/context manager.
|
2011-05-18 20:08:53 +08:00
|
|
|
|
2012-03-14 07:02:31 +08:00
|
|
|
It's actually sent twice: when the new value is applied ("setup") and when the
|
2013-05-18 19:43:51 +08:00
|
|
|
original value is restored ("teardown"). Use the ``enter`` argument to
|
|
|
|
distinguish between the two.
|
2012-03-14 07:02:31 +08:00
|
|
|
|
2014-10-09 23:46:40 +08:00
|
|
|
You can also import this signal from ``django.core.signals`` to avoid importing
|
|
|
|
from ``django.test`` in non-test situations.
|
|
|
|
|
2011-05-18 20:08:53 +08:00
|
|
|
Arguments sent with this signal:
|
|
|
|
|
|
|
|
``sender``
|
2011-07-11 03:35:05 +08:00
|
|
|
The settings handler.
|
2011-05-18 20:08:53 +08:00
|
|
|
|
|
|
|
``setting``
|
2012-03-14 07:02:31 +08:00
|
|
|
The name of the setting.
|
2011-05-18 20:08:53 +08:00
|
|
|
|
|
|
|
``value``
|
2012-03-14 07:02:31 +08:00
|
|
|
The value of the setting after the change. For settings that initially
|
|
|
|
don't exist, in the "teardown" phase, ``value`` is ``None``.
|
2011-05-18 20:08:53 +08:00
|
|
|
|
2013-05-18 19:43:51 +08:00
|
|
|
``enter``
|
2015-01-27 04:39:52 +08:00
|
|
|
A boolean; ``True`` if the setting is applied, ``False`` if restored.
|
2013-05-18 19:43:51 +08:00
|
|
|
|
2016-01-25 05:26:11 +08:00
|
|
|
``template_rendered``
|
|
|
|
---------------------
|
2008-08-27 03:04:52 +08:00
|
|
|
|
|
|
|
.. data:: django.test.signals.template_rendered
|
2008-08-27 03:23:59 +08:00
|
|
|
:module:
|
2009-09-13 07:06:22 +08:00
|
|
|
|
2008-08-27 03:04:52 +08:00
|
|
|
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:
|
|
|
|
|
2011-02-08 06:18:14 +08:00
|
|
|
``sender``
|
|
|
|
The :class:`~django.template.Template` object which was rendered.
|
2008-08-27 03:04:52 +08:00
|
|
|
|
2011-02-08 06:18:14 +08:00
|
|
|
``template``
|
|
|
|
Same as sender
|
2008-08-27 03:04:52 +08:00
|
|
|
|
2011-02-08 06:18:14 +08:00
|
|
|
``context``
|
|
|
|
The :class:`~django.template.Context` with which the template was
|
|
|
|
rendered.
|
2010-08-30 21:21:45 +08:00
|
|
|
|
|
|
|
Database Wrappers
|
|
|
|
=================
|
|
|
|
|
|
|
|
.. module:: django.db.backends
|
|
|
|
:synopsis: Core signals sent by the database wrapper.
|
|
|
|
|
|
|
|
Signals sent by the database wrapper when a database connection is
|
|
|
|
initiated.
|
|
|
|
|
2016-01-25 05:26:11 +08:00
|
|
|
``connection_created``
|
|
|
|
----------------------
|
2010-08-30 21:21:45 +08:00
|
|
|
|
|
|
|
.. data:: django.db.backends.signals.connection_created
|
|
|
|
:module:
|
|
|
|
|
|
|
|
Sent when the database wrapper makes the initial connection to the
|
|
|
|
database. This is particularly useful if you'd like to send any post
|
|
|
|
connection commands to the SQL backend.
|
|
|
|
|
|
|
|
Arguments sent with this signal:
|
|
|
|
|
2011-02-08 06:18:14 +08:00
|
|
|
``sender``
|
|
|
|
The database wrapper class -- i.e.
|
2015-08-05 22:08:56 +08:00
|
|
|
``django.db.backends.postgresql.DatabaseWrapper`` or
|
2013-01-01 21:12:42 +08:00
|
|
|
``django.db.backends.mysql.DatabaseWrapper``, etc.
|
2011-02-08 06:18:14 +08:00
|
|
|
|
|
|
|
``connection``
|
|
|
|
The database connection that was opened. This can be used in a
|
|
|
|
multiple-database configuration to differentiate connection signals
|
|
|
|
from different databases.
|