我删除对象后启动的简单信号
@receiver(post_delete, sender=Operation, dispatch_uid="delete_operation")
def delete_operation(sender, instance, **kwargs):
# Track balance operations for delete
instance.user.balance = instance.user.balance - int(instance.amount) # Note the "-"
instance.user.save()
问题在于,当我将对象删除到管理员中时,信号会启动两次并失去我的余额。我想避免2次触发。我在stackoverflow上搜索并尝试了几种解决方案,但仍无法正常工作。
以下是我导入信号的方法:
class CommonConfig(AppConfig):
name = 'sl.common'
def ready(self):
import sl.common.signals
如果有人有想法,欢迎!!
答案 0 :(得分:0)
@receiver(post_delete, sender=Operation, dispatch_uid="delete_operation")
def delete_operation(sender, instance, action, **kwargs):
if action[0:4] == 'post':
# Track balance operations for delete
instance.user.balance = instance.user.balance - int(instance.amount) # Note the "-"
instance.user.save()
每个信号都有两个动作:前置和后置 - 前置实例在更改前有字段,后置实例在更改后有字段。