连接到特定发送者发送的信号无法正常工作

时间:2018-06-28 13:13:48

标签: django django-signals

我的模型结构如下

class WalletTransactions(models.Model):
     ...
     fields here    
     ...

class WalletBalance(models.Model):
     ...
     fields here    
     ...    

如下所示的信号处理程序

@receiver(post_save, sender=WalletTransactions)
def update_balance(sender, instance, created, **kwargs):
    print instance.payment_type #field in model

最后注册

post_save.connect(update_balance, dispatch_uid=uuid.uuid4())

现在,我希望仅根据doc调用update_balance上的save时调用WalletTransaction

但是当我尝试登录到我的应用程序时,在update_balance上的save被调用时引发Session,并引发以下错误。

  / plogin /
中的

AttributeError   “会话”对象没有属性“ payment_type”

这是什么错误?

1 个答案:

答案 0 :(得分:1)

您要连接回调函数两次。 您可以将信号与@receiver post_save.connect连接。

有关更多信息,请参见此处: https://docs.djangoproject.com/en/1.11/topics/signals/#connecting-receiver-functions

此外,您没有在sender中指定post_save.connect()。因此,基本上,您将回调连接到每个对象的save方法。

要使其正常工作,只需删除以下行:

post_save.connect(update_balance, dispatch_uid=uuid.uuid4())