signals.txt 7.5 KB

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