I am trying to do something like these proposed signal decoders . In addition to having a decorator that connects a decorated method to a signal (with the signal sender as an argument to the decorator), I would like to use a decorator for class methods.
I would like to use a decorator as follows:
class ModelA(Model): @connect.post_save(ModelB) @classmethod def observe_model_b_saved(cls, sender, instance, created, **kwargs):
Decorator:
from django.db.models import signals def post_save(sender): def decorator(view): signals.post_save.connect(sender=sender, receiver=view) return view return decorator
The error I get when I do this is:
File "/Library/Python/2.6/site-packages//lib/python2.6/site-packages/django/dispatch/dispatcher.py", line 78, in connect
AssertionError: Signal receivers must be callable.
I think the problem is that @classmethod returns a class method object that cannot be called. I really donβt understand how the classmethod works under the hood, but I can guess from this man page that the class method object has not been converted to a callable before accessing it from the class, for example ModelA.observe_model_b_saved . Is there a way that I can (1) define my method as a class or instance method on a model, and (2) connect it to a signal using a decorator directly in the method definition? Thanks!
python django decorator django-models django-signals
Carl G
source share