Django pre_save 信号不起作用
我通过以下方式测试了Django的“pre_save”信号,但都无法捕获其中的信号。
$
from django.db.models.signals import pre_save
import logging
def my_callback(sender, **kwargs):
logging.debug("======================================")
pre_save.connect(my_callback)
在manage.py shell中运行上面的代码: 然后我运行我的网站,看到 models.save() 成功运行,但回调函数未运行。
或者,我再次在 shell 上运行上述代码,然后在 shell 中运行 models.save() 。 “save”再次运行良好,但回调函数仍然没有任何反应。
最后,我将上述代码嵌入到
__init__.py
文件中,并在网站上运行 save() 函数。尽管如此,什么也没发生。
您能帮我弄清楚为什么 pre_save 信号似乎不起作用吗?
I tested the "pre_save" signal of Django in the following ways, but cannot catch the signal in either of them.
$
from django.db.models.signals import pre_save
import logging
def my_callback(sender, **kwargs):
logging.debug("======================================")
pre_save.connect(my_callback)
Run the above code in manage.py shell:
Then I run my website and see models.save() work successfully, but the callback function does not run.Alternatively, I run the above code on shell again and then run models.save() in the shell. "save" works well again but still nothing happens to the callback function.
Finally, I embed the above code in an
__init__.py
file and still, run the save() function on the website. Still, nothing occurs.
Would you please help me figure out why pre_save signal seems not work?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您没有设置发送者类别。
其次,如果您使用 Django 1.3,您应该使用新的装饰器语法。
应该可以,但我还没有测试代码,所以让我知道它是否仍然损坏。
You're not setting the sender class for one.
Secondly, if you're using Django 1.3 you should use the new decorator syntax.
That should do it, but I haven't tested the code so let me know if it's still broken.
Eric 的答案使其起作用的原因是因为他让您在 models.py 中连接信号,这样当您通过网站保存模型时,信号处理程序与信号触发程序位于同一进程中。
在示例 1 和 3 中,很容易看出为什么它们不起作用 - 您正在与信号接收器正在监听的不同进程(网站)中进行保存。
我希望我能更好地理解为什么示例 2 也被破坏了,但我刚刚在 shell 中测试信号时在自己的项目中调试了类似的问题,这肯定与信号发送者和接收者无法“看到”彼此有关。
The reason Eric's answer made it work is because he got you to connect the signal inside models.py, so that when you then save the model via your website the signal handler is in the same process as the signal firer.
In examples 1 and 3 it's easy to see why they didn't work - you are saving in a different process (the website) to where your signal receivers are listening.
I wish I understood better why example 2 is also broken but I just debugged a similar issue in my own project while testing signals in the shell and it's definitely something to do with signal senders and receivers not being able to 'see' each other.
logging.debug()
使用根记录器,默认情况下处理程序级别为 30(“警告”)。=>
logging.debug('something')
只是根本不执行任何操作(DEBUG 级别为 10 < 30)。请参阅http://docs.python.org/2/library/logging.html应该使用另一个自定义记录器来完成相同的测试,或者通过执行以下操作:
原始问题没有包含足够的信息来了解这是否是OP面临的真正问题(或部分问题)。
但可以肯定的是,OP 的代码无法与我的
./manage.py shell
一起使用logging.debug()
is using the root logger, which handler level by default is 30 ('WARNING').=>
logging.debug('something')
is just not doing anything at all (DEBUG level is 10 < 30). See http://docs.python.org/2/library/logging.htmlThe same test should be done with another custom logger, or by doing:
The original question is not containing enough info to know if it's the real problem the OP was facing (or part of it).
But surely the OP's code was not going to work with my
./manage.py shell
如 django 信号文档
pre_save
信号接受 3 个唯一参数(不是关键字参数),因此,您需要将my_callback
函数编辑为如下所示:as described in the django signals doc the
pre_save
signal accept 3 unique arguments (not keyword arguments) so, you need to edit yourmy_callback
function to be as follow: