signals.txt 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  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. p = Poll(question="What's up?", pub_date=datetime.now())
  46. The arguments sent to a :data:`pre_init` handler would be:
  47. ========== ===============================================================
  48. Argument Value
  49. ========== ===============================================================
  50. ``sender`` ``Poll`` (the class itself)
  51. ``args`` ``[]`` (an empty list because there were no positional
  52. arguments passed to ``__init__``.)
  53. ``kwargs`` ``{'question': "What's up?", 'pub_date': datetime.now()}``
  54. ========== ===============================================================
  55. post_init
  56. ---------
  57. .. data:: django.db.models.signals.post_init
  58. :module:
  59. Like pre_init, but this one is sent when the :meth:`~django.db.models.Model.__init__`: method finishes.
  60. Arguments sent with this signal:
  61. ``sender``
  62. As above: the model class that just had an instance created.
  63. ``instance``
  64. The actual instance of the model that's just been created.
  65. pre_save
  66. --------
  67. .. data:: django.db.models.signals.pre_save
  68. :module:
  69. This is sent at the beginning of a model's :meth:`~django.db.models.Model.save`
  70. method.
  71. Arguments sent with this signal:
  72. ``sender``
  73. The model class.
  74. ``instance``
  75. The actual instance being saved.
  76. ``raw``
  77. A boolean; ``True`` if the model is saved exactly as presented
  78. (i.e. when loading a fixture). One should not query/modify other
  79. records in the database as the database might not be in a
  80. consistent state yet.
  81. .. versionadded:: 1.3
  82. ``using``
  83. The database alias being used.
  84. .. versionadded:: 1.5
  85. ``update_fields``
  86. The set of fields to update explicitly specified in the ``save()`` method.
  87. ``None`` if this argument was not used in the ``save()`` call.
  88. post_save
  89. ---------
  90. .. data:: django.db.models.signals.post_save
  91. :module:
  92. Like :data:`pre_save`, but sent at the end of the
  93. :meth:`~django.db.models.Model.save` method.
  94. Arguments sent with this signal:
  95. ``sender``
  96. The model class.
  97. ``instance``
  98. The actual instance being saved.
  99. ``created``
  100. A boolean; ``True`` if a new record was created.
  101. ``raw``
  102. A boolean; ``True`` if the model is saved exactly as presented
  103. (i.e. when loading a fixture). One should not query/modify other
  104. records in the database as the database might not be in a
  105. consistent state yet.
  106. .. versionadded:: 1.3
  107. ``using``
  108. The database alias being used.
  109. .. versionadded:: 1.5
  110. ``update_fields``
  111. The set of fields to update explicitly specified in the ``save()`` method.
  112. ``None`` if this argument was not used in the ``save()`` call.
  113. pre_delete
  114. ----------
  115. .. data:: django.db.models.signals.pre_delete
  116. :module:
  117. Sent at the beginning of a model's :meth:`~django.db.models.Model.delete`
  118. method and a queryset's :meth:`~django.db.models.query.QuerySet.delete` method.
  119. Arguments sent with this signal:
  120. ``sender``
  121. The model class.
  122. ``instance``
  123. The actual instance being deleted.
  124. .. versionadded:: 1.3
  125. ``using``
  126. The database alias being used.
  127. post_delete
  128. -----------
  129. .. data:: django.db.models.signals.post_delete
  130. :module:
  131. Like :data:`pre_delete`, but sent at the end of a model's
  132. :meth:`~django.db.models.Model.delete` method and a queryset's
  133. :meth:`~django.db.models.query.QuerySet.delete` method.
  134. Arguments sent with this signal:
  135. ``sender``
  136. The model class.
  137. ``instance``
  138. The actual instance being deleted.
  139. Note that the object will no longer be in the database, so be very
  140. careful what you do with this instance.
  141. .. versionadded:: 1.3
  142. ``using``
  143. The database alias being used.
  144. m2m_changed
  145. -----------
  146. .. data:: django.db.models.signals.m2m_changed
  147. :module:
  148. Sent when a :class:`ManyToManyField` is changed on a model instance.
  149. Strictly speaking, this is not a model signal since it is sent by the
  150. :class:`ManyToManyField`, but since it complements the
  151. :data:`pre_save`/:data:`post_save` and :data:`pre_delete`/:data:`post_delete`
  152. when it comes to tracking changes to models, it is included here.
  153. Arguments sent with this signal:
  154. ``sender``
  155. The intermediate model class describing the :class:`ManyToManyField`.
  156. This class is automatically created when a many-to-many field is
  157. defined; you can access it using the ``through`` attribute on the
  158. many-to-many field.
  159. ``instance``
  160. The instance whose many-to-many relation is updated. This can be an
  161. instance of the ``sender``, or of the class the :class:`ManyToManyField`
  162. is related to.
  163. ``action``
  164. A string indicating the type of update that is done on the relation.
  165. This can be one of the following:
  166. ``"pre_add"``
  167. Sent *before* one or more objects are added to the relation.
  168. ``"post_add"``
  169. Sent *after* one or more objects are added to the relation.
  170. ``"pre_remove"``
  171. Sent *before* one or more objects are removed from the relation.
  172. ``"post_remove"``
  173. Sent *after* one or more objects are removed from the relation.
  174. ``"pre_clear"``
  175. Sent *before* the relation is cleared.
  176. ``"post_clear"``
  177. Sent *after* the relation is cleared.
  178. ``reverse``
  179. Indicates which side of the relation is updated (i.e., if it is the
  180. forward or reverse relation that is being modified).
  181. ``model``
  182. The class of the objects that are added to, removed from or cleared
  183. from the relation.
  184. ``pk_set``
  185. For the ``pre_add``, ``post_add``, ``pre_remove`` and ``post_remove``
  186. actions, this is a list of primary key values that have been added to
  187. or removed from the relation.
  188. For the ``pre_clear`` and ``post_clear`` actions, this is ``None``.
  189. .. versionadded:: 1.3
  190. ``using``
  191. The database alias being used.
  192. For example, if a ``Pizza`` can have multiple ``Topping`` objects, modeled
  193. like this::
  194. class Topping(models.Model):
  195. # ...
  196. pass
  197. class Pizza(models.Model):
  198. # ...
  199. toppings = models.ManyToManyField(Topping)
  200. If we would do something like this:
  201. >>> p = Pizza.object.create(...)
  202. >>> t = Topping.objects.create(...)
  203. >>> p.toppings.add(t)
  204. the arguments sent to a :data:`m2m_changed` handler would be:
  205. ============== ============================================================
  206. Argument Value
  207. ============== ============================================================
  208. ``sender`` ``Pizza.toppings.through`` (the intermediate m2m class)
  209. ``instance`` ``p`` (the ``Pizza`` instance being modified)
  210. ``action`` ``"pre_add"`` (followed by a separate signal with ``"post_add"``)
  211. ``reverse`` ``False`` (``Pizza`` contains the :class:`ManyToManyField`,
  212. so this call modifies the forward relation)
  213. ``model`` ``Topping`` (the class of the objects added to the
  214. ``Pizza``)
  215. ``pk_set`` ``[t.id]`` (since only ``Topping t`` was added to the relation)
  216. ``using`` ``"default"`` (since the default router sends writes here)
  217. ============== ============================================================
  218. And if we would then do something like this::
  219. >>> t.pizza_set.remove(p)
  220. the arguments sent to a :data:`m2m_changed` handler would be:
  221. ============== ============================================================
  222. Argument Value
  223. ============== ============================================================
  224. ``sender`` ``Pizza.toppings.through`` (the intermediate m2m class)
  225. ``instance`` ``t`` (the ``Topping`` instance being modified)
  226. ``action`` ``"pre_remove"`` (followed by a separate signal with ``"post_remove"``)
  227. ``reverse`` ``True`` (``Pizza`` contains the :class:`ManyToManyField`,
  228. so this call modifies the reverse relation)
  229. ``model`` ``Pizza`` (the class of the objects removed from the
  230. ``Topping``)
  231. ``pk_set`` ``[p.id]`` (since only ``Pizza p`` was removed from the
  232. relation)
  233. ``using`` ``"default"`` (since the default router sends writes here)
  234. ============== ============================================================
  235. class_prepared
  236. --------------
  237. .. data:: django.db.models.signals.class_prepared
  238. :module:
  239. Sent whenever a model class has been "prepared" -- that is, once model has
  240. been defined and registered with Django's model system. Django uses this
  241. signal internally; it's not generally used in third-party applications.
  242. Arguments that are sent with this signal:
  243. ``sender``
  244. The model class which was just prepared.
  245. Management signals
  246. ==================
  247. Signals sent by :doc:`django-admin </ref/django-admin>`.
  248. post_syncdb
  249. -----------
  250. .. data:: django.db.models.signals.post_syncdb
  251. :module:
  252. Sent by the :djadmin:`syncdb` command after it installs an application, and the
  253. :djadmin:`flush` command.
  254. Any handlers that listen to this signal need to be written in a particular
  255. place: a ``management`` module in one of your :setting:`INSTALLED_APPS`. If
  256. handlers are registered anywhere else they may not be loaded by
  257. :djadmin:`syncdb`. It is important that handlers of this signal perform
  258. idempotent changes (e.g. no database alterations) as this may cause the
  259. :djadmin:`flush` management command to fail if it also ran during the
  260. :djadmin:`syncdb` command.
  261. Arguments sent with this signal:
  262. ``sender``
  263. The ``models`` module that was just installed. That is, if
  264. :djadmin:`syncdb` just installed an app called ``"foo.bar.myapp"``,
  265. ``sender`` will be the ``foo.bar.myapp.models`` module.
  266. ``app``
  267. Same as ``sender``.
  268. ``created_models``
  269. A list of the model classes from any app which :djadmin:`syncdb` has
  270. created so far.
  271. ``verbosity``
  272. Indicates how much information manage.py is printing on screen. See
  273. the :djadminopt:`--verbosity` flag for details.
  274. Functions which listen for :data:`post_syncdb` should adjust what they
  275. output to the screen based on the value of this argument.
  276. ``interactive``
  277. If ``interactive`` is ``True``, it's safe to prompt the user to input
  278. things on the command line. If ``interactive`` is ``False``, functions
  279. which listen for this signal should not try to prompt for anything.
  280. For example, the :mod:`django.contrib.auth` app only prompts to create a
  281. superuser when ``interactive`` is ``True``.
  282. For example, ``yourapp/management/__init__.py`` could be written like::
  283. from django.db.models.signals import post_syncdb
  284. import yourapp.models
  285. def my_callback(sender, **kwargs):
  286. # Your specific logic here
  287. pass
  288. post_syncdb.connect(my_callback, sender=yourapp.models)
  289. Request/response signals
  290. ========================
  291. .. module:: django.core.signals
  292. :synopsis: Core signals sent by the request/response system.
  293. Signals sent by the core framework when processing a request.
  294. request_started
  295. ---------------
  296. .. data:: django.core.signals.request_started
  297. :module:
  298. Sent when Django begins processing an HTTP request.
  299. Arguments sent with this signal:
  300. ``sender``
  301. The handler class -- e.g.
  302. :class:`django.core.handlers.wsgi.WsgiHandler` -- that handled
  303. the request.
  304. request_finished
  305. ----------------
  306. .. data:: django.core.signals.request_finished
  307. :module:
  308. Sent when Django finishes processing an HTTP request.
  309. Arguments sent with this signal:
  310. ``sender``
  311. The handler class, as above.
  312. got_request_exception
  313. ---------------------
  314. .. data:: django.core.signals.got_request_exception
  315. :module:
  316. This signal is sent whenever Django encounters an exception while processing an incoming HTTP request.
  317. Arguments sent with this signal:
  318. ``sender``
  319. The handler class, as above.
  320. ``request``
  321. The :class:`~django.http.HttpRequest` object.
  322. Test signals
  323. ============
  324. .. module:: django.test.signals
  325. :synopsis: Signals sent during testing.
  326. Signals only sent when :doc:`running tests </topics/testing>`.
  327. setting_changed
  328. ---------------
  329. .. versionadded:: 1.4
  330. .. data:: django.test.signals.setting_changed
  331. :module:
  332. This signal is sent when the value of a setting is changed through the
  333. :meth:`django.test.TestCase.setting` context manager or the
  334. :func:`django.test.utils.override_settings` decorator/context manager.
  335. It's actually sent twice: when the new value is applied ("setup") and when the
  336. original value is restored ("teardown").
  337. Arguments sent with this signal:
  338. ``sender``
  339. The settings handler.
  340. ``setting``
  341. The name of the setting.
  342. ``value``
  343. The value of the setting after the change. For settings that initially
  344. don't exist, in the "teardown" phase, ``value`` is ``None``.
  345. template_rendered
  346. -----------------
  347. .. data:: django.test.signals.template_rendered
  348. :module:
  349. Sent when the test system renders a template. This signal is not emitted during
  350. normal operation of a Django server -- it is only available during testing.
  351. Arguments sent with this signal:
  352. ``sender``
  353. The :class:`~django.template.Template` object which was rendered.
  354. ``template``
  355. Same as sender
  356. ``context``
  357. The :class:`~django.template.Context` with which the template was
  358. rendered.
  359. Database Wrappers
  360. =================
  361. .. module:: django.db.backends
  362. :synopsis: Core signals sent by the database wrapper.
  363. Signals sent by the database wrapper when a database connection is
  364. initiated.
  365. connection_created
  366. ------------------
  367. .. data:: django.db.backends.signals.connection_created
  368. :module:
  369. Sent when the database wrapper makes the initial connection to the
  370. database. This is particularly useful if you'd like to send any post
  371. connection commands to the SQL backend.
  372. Arguments sent with this signal:
  373. ``sender``
  374. The database wrapper class -- i.e.
  375. :class:`django.db.backends.postgresql_psycopg2.DatabaseWrapper` or
  376. :class:`django.db.backends.mysql.DatabaseWrapper`, etc.
  377. ``connection``
  378. The database connection that was opened. This can be used in a
  379. multiple-database configuration to differentiate connection signals
  380. from different databases.