我有以下信号。是否可以像我在这里那样“堆叠”这两个装饰器?
@receiver(signal=charge_succeeded)
@transaction.atomic
def create_influencer_transaction(sender, order, charge, **kwargs):
pass
答案 0 :(得分:7)
是的
考虑装饰器的工作原理,这等效于
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
将返回具有原子性包装器逻辑的新函数,但是receiver
将仅返回相同的函数。
但是,这里的订单确实很重要(而且您做对了);如果装饰器相反,则receiver
将注册非原子版本,这是不好的。