Created
August 8, 2017 20:55
-
-
Save ezheidtmann/85fcea99ee4c5c070cbea41ed921ea8e to your computer and use it in GitHub Desktop.
Receive Django signals for a model (workaround for bug in built-in sender filtering)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def model_instance_signal_receiver(signal, Model): | |
"""Register signal receiver for a specific model | |
When using QuerySet.defer(), the model objects are instances of a subclass, so | |
the built-in sender matching doesn't work. This approach uses that subclassing | |
to ensure we run the receiver for both deferred and non-deferred instances. | |
As a side effect, this also allows you to register a receiver for all | |
subclasses of an abstract model. | |
""" | |
from django.dispatch import receiver | |
def decorator(fn): | |
@receiver(signal) | |
@wraps(fn) | |
def wrapper(sender, **kwargs): | |
if issubclass(sender, Model): | |
instance = kwargs.pop('instance') | |
if instance is not None: | |
kwargs['sender'] = sender | |
return fn(instance, **kwargs) | |
return wrapper | |
return decorator |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example usage: