发送Django自定义连接信号,但方法没有运行..如何修复?

时间:2011-12-11 16:57:05

标签: django signals

我有一个未运行的自定义连接信号:

方法add_participant中的竞赛模型中的代码:

            # this is called and no error happens
            contest_after_added_participant.send(sender=self, 
                                           participant=participant, 
                                           participation=participation)

竞赛模型存在的文件中的代码:

def my_callback(sender, **kwargs):
    sender.title += 'sss' # this is never called

contest_after_added_participant = Signal(providing_args=["participant", "participation"])
contest_after_added_participant.connect(my_callback, sender=Contest, dispatch_uid='Contest.001')

2 个答案:

答案 0 :(得分:2)

发送发件人时,kwarg应该是比赛,而不是比赛的实例。检查:

contest_after_added_participant.send(sender=Contest, 
                                 participant=participant, 
                                 participation=participation)

答案 1 :(得分:1)

你的错误就是你调用了.connect(),其中“发件人”参数是比赛,而.send()则调用了竞赛实例,所以他们不匹配。如果您只有一个自定义信号的侦听器,并且不需要过滤特定发件人发送的信号,如此处所述:https://docs.djangoproject.com/en/dev/topics/signals/#connecting-to-signals-sent-by-specific-senders,那么您也可以从.connect()调用中删除“sender”参数:

contest_after_added_participant.connect(my_callback, dispatch_uid='Contest.001')