Django在管理中完成操作时发出两次信号

2024-04-27 03:12:44 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个简单的信号,当对象被删除时就会启动

@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并尝试了几种解决方案,但仍然没有成功。在

以下是如何导入信号:

在应用程序.py在

^{pr2}$

如果有人有主意的话,欢迎!!在


Tags: 对象instanceuid信号defdeletepostoperation
1条回答
网友
1楼 · 发布于 2024-04-27 03:12:44
@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()

每个信号有两个动作:前实例和后实例在更改前有字段,后实例在更改后有字段。在

  • 前加和后加
  • 移除前和移除后

相关问题 更多 >