Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django: Stacking decorators

Tags:

python

django

I have the following signal. Is it possible to 'stack' these two decorators as I did here?

@receiver(signal=charge_succeeded)
@transaction.atomic
def create_influencer_transaction(sender, order, charge, **kwargs):
    pass
like image 861
Joey Coder Avatar asked Apr 03 '26 03:04

Joey Coder


1 Answers

Yes.

Considering how decorators work, this is equivalent to

def create_influencer_transaction(sender, order, charge, **kwargs):
    pass


create_influencer_transaction = transaction.atomic(
    create_influencer_transaction
)
create_influencer_transaction = receiver(signal=charge_succeeded)(
    create_influencer_transaction
)

transaction.atomic will return a new function that has the atomicity wrapper logic, but receiver will just return the same function.

However, order here does matter (and you've got it right); if the decorators were the other way around, receiver would register the non-atomic version, which is not good.

like image 170
AKX Avatar answered Apr 08 '26 07:04

AKX



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!