longedok
longedok

Reputation: 86

Connect a signal to a bound method

I'm trying to connect django's post_save signal to a bound instance method, but it doesn't get called. The code is:

for adapter_class in signal_adapters:
    adapater_instance = adapter_class(definition_class)
    for signal in adapter_class.signals:
        signal.connect(adapater_instance.signal_reciever, sender=action_object_class)

If I decorate signal_receiver with @classmethod it works ok. There's this comment in the source of the connect method, which led me to believe it should be possible to connect bound methods as well:

receiver - A function or an instance method which is to receive signals.

I guess I could curry the static version of signal_receiver, passing it instance as a parameter, and use this as a receiver, but is there a better way?

Upvotes: 1

Views: 127

Answers (1)

Mohammad Mustaqeem
Mohammad Mustaqeem

Reputation: 1084

You need to also pass "weak=False" in connect method. So your code should be like:

for adapter_class in signal_adapters:
    adapater_instance = adapter_class(definition_class)
    for signal in adapter_class.signals:
        signal.connect(adapater_instance.signal_reciever, sender=action_object_class, weak=False)

Upvotes: 3

Related Questions