Question

in a Django shop application there gets registered signal handler to some action like adding an item to the cart.

I'd like to replace this handler with my own version in localsite/models.py, ie. without touching original sources.

If just calling connect method

signals.satchmo_cart_add_verify.connect(my_veto_out_of_stock)

the custom handler appends to the list of current recievers and the original still gets an action:

print signals.satchmo_cart_add_verify.receivers
"""
[((140073113515864, 140073319632416), <weakref at 0x7f65502c1aa0;
to 'function' at 0x7f65502c7758 (veto_out_of_stock)>),
((140073114981632, 140073319632416), <weakref at 0x7f65504295d0;
to 'function' at 0x7f655042d500 (my_veto_out_of_stock)>)]
"""

I can in an advance remove the original handlers with

for hnd in signals.satchmo_cart_add_verify.receivers:
    del hnd

but find it ugly and hackish.

So what's the proper way to replace the signal handler ?

Thanks

Était-ce utile?

La solution

Have your tried Signal.disconnect?

signals.satchmo_cart_add_verify.disconnect(
    signals.satchmo_cart_add_verify.receivers[0][1]())

This way is clear IMO.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top