signals.txt 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  1. =======
  2. Signals
  3. =======
  4. A list of all the signals that Django sends.
  5. .. seealso::
  6. See the documentation on the :doc:`signal dispatcher </topics/signals>` for
  7. information regarding how to register for and receive signals.
  8. The :doc:`comment framework </ref/contrib/comments/index>` sends a :doc:`set
  9. of comment-related signals </ref/contrib/comments/signals>`.
  10. The :doc:`authentication framework </topics/auth>` sends :ref:`signals when
  11. a user is logged in / out <topics-auth-signals>`.
  12. Model signals
  13. =============
  14. .. module:: django.db.models.signals
  15. :synopsis: Signals sent by the model system.
  16. The :mod:`django.db.models.signals` module defines a set of signals sent by the
  17. module system.
  18. .. warning::
  19. Many of these signals are sent by various model methods like
  20. :meth:`~django.db.models.Model.__init__` or
  21. :meth:`~django.db.models.Model.save` that you can overwrite in your own
  22. code.
  23. If you override these methods on your model, you must call the parent class'
  24. methods for this signals to be sent.
  25. Note also that Django stores signal handlers as weak references by default,
  26. so if your handler is a local function, it may be garbage collected. To
  27. prevent this, pass ``weak=False`` when you call the signal's :meth:`~django.dispatch.Signal.connect`.
  28. pre_init
  29. --------
  30. .. attribute:: django.db.models.signals.pre_init
  31. :module:
  32. .. ^^^^^^^ this :module: hack keeps Sphinx from prepending the module.
  33. Whenever you instantiate a Django model,, this signal is sent at the beginning
  34. of the model's :meth:`~django.db.models.Model.__init__` method.
  35. Arguments sent with this signal:
  36. ``sender``
  37. The model class that just had an instance created.
  38. ``args``
  39. A list of positional arguments passed to
  40. :meth:`~django.db.models.Model.__init__`:
  41. ``kwargs``
  42. A dictionary of keyword arguments passed to
  43. :meth:`~django.db.models.Model.__init__`:.
  44. For example, the :doc:`tutorial </intro/tutorial01>` has this line:
  45. .. code-block:: python
  46. p = Poll(question="What's up?", pub_date=datetime.now())
  47. The arguments sent to a :data:`pre_init` handler would be:
  48. ========== ===============================================================
  49. Argument Value
  50. ========== ===============================================================
  51. ``sender`` ``Poll`` (the class itself)
  52. ``args`` ``[]`` (an empty list because there were no positional
  53. arguments passed to ``__init__``.)
  54. ``kwargs`` ``{'question': "What's up?", 'pub_date': datetime.now()}``
  55. ========== ===============================================================
  56. post_init
  57. ---------
  58. .. data:: django.db.models.signals.post_init
  59. :module:
  60. Like pre_init, but this one is sent when the :meth:`~django.db.models.Model.__init__`: method finishes.
  61. Arguments sent with this signal:
  62. ``sender``
  63. As above: the model class that just had an instance created.
  64. ``instance``
  65. The actual instance of the model that's just been created.
  66. pre_save
  67. --------
  68. .. data:: django.db.models.signals.pre_save
  69. :module:
  70. This is sent at the beginning of a model's :meth:`~django.db.models.Model.save`
  71. method.
  72. Arguments sent with this signal:
  73. ``sender``
  74. The model class.
  75. ``instance``
  76. The actual instance being saved.
  77. .. versionadded:: 1.3
  78. ``using``
  79. The database alias being used.
  80. post_save
  81. ---------
  82. .. data:: django.db.models.signals.post_save
  83. :module:
  84. Like :data:`pre_save`, but sent at the end of the
  85. :meth:`~django.db.models.Model.save` method.
  86. Arguments sent with this signal:
  87. ``sender``
  88. The model class.
  89. ``instance``
  90. The actual instance being saved.
  91. ``created``
  92. A boolean; ``True`` if a new record was created.
  93. .. versionadded:: 1.3
  94. ``using``
  95. The database alias being used.
  96. pre_delete
  97. ----------
  98. .. data:: django.db.models.signals.pre_delete
  99. :module:
  100. Sent at the beginning of a model's :meth:`~django.db.models.Model.delete`
  101. method and a queryset's :meth:`~django.db.models.query.QuerySet.delete` method.
  102. Arguments sent with this signal:
  103. ``sender``
  104. The model class.
  105. ``instance``
  106. The actual instance being deleted.
  107. .. versionadded:: 1.3
  108. ``using``
  109. The database alias being used.
  110. post_delete
  111. -----------
  112. .. data:: django.db.models.signals.post_delete
  113. :module:
  114. Like :data:`pre_delete`, but sent at the end of a model's
  115. :meth:`~django.db.models.Model.delete` method and a queryset's
  116. :meth:`~django.db.models.query.QuerySet.delete` method.
  117. Arguments sent with this signal:
  118. ``sender``
  119. The model class.
  120. ``instance``
  121. The actual instance being deleted.
  122. Note that the object will no longer be in the database, so be very
  123. careful what you do with this instance.
  124. .. versionadded:: 1.3
  125. ``using``
  126. The database alias being used.
  127. m2m_changed
  128. -----------
  129. .. data:: django.db.models.signals.m2m_changed
  130. :module:
  131. .. versionadded:: 1.2
  132. Sent when a :class:`ManyToManyField` is changed on a model instance.
  133. Strictly speaking, this is not a model signal since it is sent by the
  134. :class:`ManyToManyField`, but since it complements the
  135. :data:`pre_save`/:data:`post_save` and :data:`pre_delete`/:data:`post_delete`
  136. when it comes to tracking changes to models, it is included here.
  137. Arguments sent with this signal:
  138. ``sender``
  139. The intermediate model class describing the :class:`ManyToManyField`.
  140. This class is automatically created when a many-to-many field is
  141. defined; you can access it using the ``through`` attribute on the
  142. many-to-many field.
  143. ``instance``
  144. The instance whose many-to-many relation is updated. This can be an
  145. instance of the ``sender``, or of the class the :class:`ManyToManyField`
  146. is related to.
  147. ``action``
  148. A string indicating the type of update that is done on the relation.
  149. This can be one of the following:
  150. ``"pre_add"``
  151. Sent *before* one or more objects are added to the relation
  152. ``"post_add"``
  153. Sent *after* one or more objects are added to the relation
  154. ``"pre_remove"``
  155. Sent *after* one or more objects are removed from the relation
  156. ``"post_remove"``
  157. Sent *after* one or more objects are removed from the relation
  158. ``"pre_clear"``
  159. Sent *before* the relation is cleared
  160. ``"post_clear"``
  161. Sent *after* the relation is cleared
  162. ``reverse``
  163. Indicates which side of the relation is updated (i.e., if it is the
  164. forward or reverse relation that is being modified).
  165. ``model``
  166. The class of the objects that are added to, removed from or cleared
  167. from the relation.
  168. ``pk_set``
  169. For the ``pre_add``, ``post_add``, ``pre_remove`` and ``post_remove``
  170. actions, this is a list of primary key values that have been added to
  171. or removed from the relation.
  172. For the ``pre_clear`` and ``post_clear`` actions, this is ``None``.
  173. .. versionadded:: 1.3
  174. ``using``
  175. The database alias being used.
  176. For example, if a ``Pizza`` can have multiple ``Topping`` objects, modeled
  177. like this:
  178. .. code-block:: python
  179. class Topping(models.Model):
  180. # ...
  181. class Pizza(models.Model):
  182. # ...
  183. toppings = models.ManyToManyField(Topping)
  184. If we would do something like this:
  185. .. code-block:: python
  186. >>> p = Pizza.object.create(...)
  187. >>> t = Topping.objects.create(...)
  188. >>> p.toppings.add(t)
  189. the arguments sent to a :data:`m2m_changed` handler would be:
  190. ============== ============================================================
  191. Argument Value
  192. ============== ============================================================
  193. ``sender`` ``Pizza.toppings.through`` (the intermediate m2m class)
  194. ``instance`` ``p`` (the ``Pizza`` instance being modified)
  195. ``action`` ``"pre_add"`` (followed by a separate signal with ``"post_add"``)
  196. ``reverse`` ``False`` (``Pizza`` contains the :class:`ManyToManyField`,
  197. so this call modifies the forward relation)
  198. ``model`` ``Topping`` (the class of the objects added to the
  199. ``Pizza``)
  200. ``pk_set`` ``[t.id]`` (since only ``Topping t`` was added to the relation)
  201. ``using`` ``"default"`` (since the default router sends writes here)
  202. ============== ============================================================
  203. And if we would then do something like this:
  204. .. code-block:: python
  205. >>> t.pizza_set.remove(p)
  206. the arguments sent to a :data:`m2m_changed` handler would be:
  207. ============== ============================================================
  208. Argument Value
  209. ============== ============================================================
  210. ``sender`` ``Pizza.toppings.through`` (the intermediate m2m class)
  211. ``instance`` ``t`` (the ``Topping`` instance being modified)
  212. ``action`` ``"pre_remove"`` (followed by a separate signal with ``"post_remove"``)
  213. ``reverse`` ``True`` (``Pizza`` contains the :class:`ManyToManyField`,
  214. so this call modifies the reverse relation)
  215. ``model`` ``Pizza`` (the class of the objects removed from the
  216. ``Topping``)
  217. ``pk_set`` ``[p.id]`` (since only ``Pizza p`` was removed from the
  218. relation)
  219. ``using`` ``"default"`` (since the default router sends writes here)
  220. ============== ============================================================
  221. class_prepared
  222. --------------
  223. .. data:: django.db.models.signals.class_prepared
  224. :module:
  225. Sent whenever a model class has been "prepared" -- that is, once model has
  226. been defined and registered with Django's model system. Django uses this
  227. signal internally; it's not generally used in third-party applications.
  228. Arguments that are sent with this signal:
  229. ``sender``
  230. The model class which was just prepared.
  231. Management signals
  232. ==================
  233. Signals sent by :doc:`django-admin </ref/django-admin>`.
  234. post_syncdb
  235. -----------
  236. .. data:: django.db.models.signals.post_syncdb
  237. :module:
  238. Sent by :djadmin:`syncdb` after it installs an application.
  239. Any handlers that listen to this signal need to be written in a particular
  240. place: a ``management`` module in one of your :setting:`INSTALLED_APPS`. If
  241. handlers are registered anywhere else they may not be loaded by
  242. :djadmin:`syncdb`.
  243. Arguments sent with this signal:
  244. ``sender``
  245. The ``models`` module that was just installed. That is, if
  246. :djadmin:`syncdb` just installed an app called ``"foo.bar.myapp"``,
  247. ``sender`` will be the ``foo.bar.myapp.models`` module.
  248. ``app``
  249. Same as ``sender``.
  250. ``created_models``
  251. A list of the model classes from any app which :djadmin:`syncdb` has
  252. created so far.
  253. ``verbosity``
  254. Indicates how much information manage.py is printing on screen. See
  255. the :djadminopt:`--verbosity` flag for details.
  256. Functions which listen for :data:`post_syncdb` should adjust what they
  257. output to the screen based on the value of this argument.
  258. ``interactive``
  259. If ``interactive`` is ``True``, it's safe to prompt the user to input
  260. things on the command line. If ``interactive`` is ``False``, functions
  261. which listen for this signal should not try to prompt for anything.
  262. For example, the :mod:`django.contrib.auth` app only prompts to create a
  263. superuser when ``interactive`` is ``True``.
  264. For example, yourapp/signals/__init__.py could be written like::
  265. from django.db.models.signals import post_syncdb
  266. import yourapp.models
  267. def my_callback(sender, **kwargs):
  268. # Your specific logic here
  269. pass
  270. post_syncdb.connect(my_callback, sender=yourapp.models)
  271. Request/response signals
  272. ========================
  273. .. module:: django.core.signals
  274. :synopsis: Core signals sent by the request/response system.
  275. Signals sent by the core framework when processing a request.
  276. request_started
  277. ---------------
  278. .. data:: django.core.signals.request_started
  279. :module:
  280. Sent when Django begins processing an HTTP request.
  281. Arguments sent with this signal:
  282. ``sender``
  283. The handler class -- e.g.
  284. :class:`django.core.handlers.wsgi.WsgiHandler` -- that handled
  285. the request.
  286. request_finished
  287. ----------------
  288. .. data:: django.core.signals.request_finished
  289. :module:
  290. Sent when Django finishes processing an HTTP request.
  291. Arguments sent with this signal:
  292. ``sender``
  293. The handler class, as above.
  294. got_request_exception
  295. ---------------------
  296. .. data:: django.core.signals.got_request_exception
  297. :module:
  298. This signal is sent whenever Django encounters an exception while processing an incoming HTTP request.
  299. Arguments sent with this signal:
  300. ``sender``
  301. The handler class, as above.
  302. ``request``
  303. The :class:`~django.http.HttpRequest` object.
  304. Test signals
  305. ============
  306. .. module:: django.test.signals
  307. :synopsis: Signals sent during testing.
  308. Signals only sent when :doc:`running tests </topics/testing>`.
  309. template_rendered
  310. -----------------
  311. .. data:: django.test.signals.template_rendered
  312. :module:
  313. Sent when the test system renders a template. This signal is not emitted during
  314. normal operation of a Django server -- it is only available during testing.
  315. Arguments sent with this signal:
  316. ``sender``
  317. The :class:`~django.template.Template` object which was rendered.
  318. ``template``
  319. Same as sender
  320. ``context``
  321. The :class:`~django.template.Context` with which the template was
  322. rendered.
  323. Database Wrappers
  324. =================
  325. .. module:: django.db.backends
  326. :synopsis: Core signals sent by the database wrapper.
  327. Signals sent by the database wrapper when a database connection is
  328. initiated.
  329. connection_created
  330. ------------------
  331. .. data:: django.db.backends.signals.connection_created
  332. :module:
  333. .. versionchanged:: 1.2
  334. The connection argument was added
  335. Sent when the database wrapper makes the initial connection to the
  336. database. This is particularly useful if you'd like to send any post
  337. connection commands to the SQL backend.
  338. Arguments sent with this signal:
  339. ``sender``
  340. The database wrapper class -- i.e.
  341. :class:`django.db.backends.postgresql_psycopg2.DatabaseWrapper` or
  342. :class:`django.db.backends.mysql.DatabaseWrapper`, etc.
  343. ``connection``
  344. The database connection that was opened. This can be used in a
  345. multiple-database configuration to differentiate connection signals
  346. from different databases.