信号处理程序应该在django项目中生活在哪里? [英] Where should signal handlers live in a django project?

查看:105
本文介绍了信号处理程序应该在django项目中生活在哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始在django项目中实现信号监听器。虽然我明白他们是什么,以及如何使用它们。我很难弄清楚我应该把它放在哪里。来自django网站的文档可以这样说:


这段代码应该在哪里?



你可以把信号处理和
任何你喜欢的注册码。
但是,您需要确保
的早期
导入的模块,以便在任何信号需要
之前,信号处理获得
注册被发送。这使得您的应用程序的
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:

Where should this code live?

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆