signals.txt 13 KB

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