无法使post_save在Django中工作

时间:2013-06-23 05:14:57

标签: python django django-signals

我阅读了关于django docs的{​​{1}},并为我的模型signals编写了这段代码:

Car

但问题是,当我启动服务器并打开管理员时,我的终端中会收到很多@receiver(request_finished) def signal_callback(sender, **kwargs): print 'Save Signal received' @receiver(post_save, sender=Car) def signal_handler(sender, **kwargs): pass request_finished(signal_callback, sender=car, dispatch_url="Unique save id") 。我想知道的是,我仅将'Save Signal received'限制为signal_handler。但是,即使没有保存任何内容,该消息也会出现很多次。我不明白这一点。

注意:我会诚实的。我理解了部分内容,而不是文档中的所有内容。

2 个答案:

答案 0 :(得分:5)

有一种更简单的方法来绑定post_save signals

from django.db.models.signals import post_save
from myapp.models import Car

def do_something(sender, **kwargs):
    print 'the object is now saved.'
    car = kwargs['instance'] #now i have access to the object

post_save.connect(do_something, sender=Car)

信号request finished被称为时间发出一个HTTP请求,这是一种生猪。

答案 1 :(得分:1)

您已将request_finished信号绑定到signal_callback。删除(或注释掉)signal_callback,然后按照以下方式更改signal_handler

@receiver(post_save, sender=Car)
def signal_handler(sender, **kwargs):
    print 'Save signal received'