信号处理程序应该在django项目中生活在哪里? [英] Where should signal handlers live in a django project?
问题描述
你可以把信号处理和
任何你喜欢的注册码。
但是,您需要确保
的早期
导入的模块,以便在任何信号需要
之前,信号处理获得
注册被发送。这使得您的应用程序的
models.py成为放置
注册信号处理程序的好地方。
虽然它的一个很好的建议,在我的models.py中使用非模型类或方法只是使我错误的方式。
那么,最好的做法/规则是存储和注册信号处理程序?
我实际上喜欢使他们的模型本身的类方法。这样可以将所有内容都保留在一个类中,这意味着您不用担心导入任何内容。
I have just started implementing signal listeners in a django project. While I understand what they are and how to use them. I am having a hard time figuring out where I should put them. The documentation from the django site has this to say:
You can put signal handling and registration code anywhere you like. However, you'll need to make sure that the module it's in gets imported early on so that the signal handling gets registered before any signals need to be sent. This makes your app's models.py a good place to put registration of signal handlers.
While its a good suggestion, having non model classes or methods in my models.py just rubs me the wrong way.
So then, what is the best practice/rule for storing and registering signal handlers?
I actually like to make them classmethods of the model itself. That keeps everything within one class, and means you don't have to worry about importing anything.
这篇关于信号处理程序应该在django项目中生活在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!