Fixed #21391 -- Allow model signals to lazily reference their senders.

This commit is contained in:
Simon Charette 2013-11-04 23:11:51 -05:00
parent 03bc0a8ac5
commit eb38257e51
7 changed files with 197 additions and 15 deletions

View file

@ -22,7 +22,7 @@ Model signals
:synopsis: Signals sent by the model system.
The :mod:`django.db.models.signals` module defines a set of signals sent by the
module system.
model system.
.. warning::
@ -37,6 +37,14 @@ module system.
so if your handler is a local function, it may be garbage collected. To
prevent this, pass ``weak=False`` when you call the signal's :meth:`~django.dispatch.Signal.connect`.
.. versionadded:: 1.7
Model signals ``sender`` model can be lazily referenced when connecting a
receiver by specifying its full application label. For example, an
``Answer`` model defined in the ``polls`` application could be referenced
as ``'polls.Answer'``. This sort of reference can be quite handy when
dealing with circular import dependencies and swappable models.
pre_init
--------

View file

@ -425,7 +425,7 @@ Models
* Is it now possible to avoid creating a backward relation for
:class:`~django.db.models.OneToOneField` by setting its
:attr:`~django.db.models.ForeignKey.related_name` to
`'+'` or ending it with `'+'`.
``'+'`` or ending it with ``'+'``.
* :class:`F expressions <django.db.models.F>` support the power operator
(``**``).
@ -436,6 +436,10 @@ Signals
* The ``enter`` argument was added to the
:data:`~django.test.signals.setting_changed` signal.
* The model signals can be now be connected to using a ``str`` of the
``'app_label.ModelName'`` form just like related fields to lazily
reference their senders.
Templates
^^^^^^^^^

View file

@ -413,6 +413,19 @@ different User model.
class Article(models.Model):
author = models.ForeignKey(settings.AUTH_USER_MODEL)
.. versionadded:: 1.7
When connecting to signals sent by the User model, you should specify the
custom model using the :setting:`AUTH_USER_MODEL` setting. For example::
from django.conf import settings
from django.db.models.signals import post_save
def post_save_receiver(signal, sender, instance, **kwargs):
pass
post_save.connect(post_save_receiver, sender=settings.AUTH_USER_MODEL)
Specifying a custom User model
------------------------------