signals.txt 7.8 KB

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