信号处理程序应该位于 django 项目中的什么位置?
我刚刚开始在 django 项目中实现信号监听器。虽然我了解它们是什么以及如何使用它们。我很难弄清楚应该把它们放在哪里。 django 站点的文档是这样说的:
此代码应该放在哪里?< /p>
您可以将信号处理和 注册码可以放在任何你喜欢的地方。 但是,您需要确保 它所在的模块会提前导入 以便信号处理得到 在任何信号需要之前注册 被发送。这使得您的应用程序 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
这是在 Django 1.7 时添加到文档中的被释放:
最佳实践是在信号子模块的 handlers.py 中定义处理程序,例如一个如下所示的文件:
yourapp/signals/handlers.py:
注册信号处理程序的最佳位置是在使用 ready() 方法定义它的应用程序的 AppConfig。这看起来像这样:
yourapp/apps.py:
确保通过直接在 settings.py 的 INSTALLED_APPS 中或在
__init__
中指定来加载 AppConfig。您的应用程序的。请参阅查看ready()文档更多信息。注意:如果您也提供信号供其他应用程序监听,请将它们放入信号模块的
__init__
中,例如如下所示的文件:>yourapp/signals/_init_.py
然后,另一个应用程序可以通过导入和注册来监听您的信号,例如
from yourapp.signals import task_generate_pre_save
。将信号与处理程序分开可以使事情保持干净。Django 1.6 说明:
如果您仍然停留在 Django 1.6 或更低版本上,那么您可以做同样的事情(在 yourapp/signals/handlers.py 中定义处理程序),而不是使用 AppConfig ,您可以通过应用程序的 _init_.py 加载处理程序,例如:
yourapp/_init_.py
这不是'与使用ready()方法一样好,因为它经常导致循环导入问题。
This was added to the documentation when Django 1.7 was released:
Best practice is to define your handlers in handlers.py in a signals submodule, e.g. a file that looks like:
yourapp/signals/handlers.py:
The best place to register your signal handler is then in the AppConfig of the app that defines it, using the ready() method. This will look like this:
yourapp/apps.py:
Make sure you're loading your AppConfig by specifying it either directly in your settings.py's INSTALLED_APPS, or in the
__init__
of your app. See see the ready() documentation for more information.Note: If you're providing signals for other apps to listen too as well, put them in the
__init__
in your signals module, e.g. a file that looks like:yourapp/signals/_init_.py
Another app can then listen to your signal by importing and registering it, e.g.
from yourapp.signals import task_generate_pre_save
. Separating your signals from your handlers keeps things clean.Instructions for Django 1.6:
If you're still stuck on Django 1.6 or lower, then you'd do the same thing (define your handlers in yourapp/signals/handlers.py) but rather than using AppConfig, you would load the handlers via the _init_.py of your app, e.g. something like:
yourapp/_init_.py
This isn't as nice as using the ready() method because it often causes circular import issues.
我实际上喜欢让它们成为模型本身的类方法。这将所有内容都保留在一个类中,这意味着您不必担心导入任何内容。
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.contrib.auth.signals 。
我已将信号处理程序放入
signals.py
文件中,然后从__init__.py
模块文件导入信号,因为我相信应用程序一启动就会调用它启动(使用print
语句进行测试表明它甚至在读取设置文件之前就被调用。)并且在signals.py中
我对Django(/python)非常陌生,所以我对任何告诉我的人持开放态度我认为这是一个糟糕的主意!
I've only just come across this, and as my signals are not model-related I thought I'd add my solution.
I am logging various data around log in / log out, and needed to hook into
django.contrib.auth.signals
.I have put the signal handlers into a
signals.py
file, and then imported signals from the__init__.py
module file, as I believe this is called as soon as the app starts up (testing with aprint
statement suggests that it's called even before the settings file is read.)and in signals.py
I'm pretty new to Django (/python) so am open to anyone telling me that this is a terrible idea!
我最近刚刚读过这个 关于布局项目/应用程序的最佳实践的文章,它建议所有自定义调度程序信号都应放入名为
signals.py
的文件中。但是,这并不能完全解决您的问题,因为您仍然需要将它们导入到某个地方,并且越早导入越好。模型建议是一个很好的模型。由于您已经在
signals.py
文件中定义了所有内容,因此文件顶部的内容不应超过一行。这类似于admin.py
文件的布局方式(类定义位于顶部,用于注册所有自定义管理类的代码位于底部),如果您定义信号然后连接它们在同一个文件中。希望有帮助!最终取决于您的喜好。
I just recently read this article about best practices when it comes to lay out your projects/applications, and it suggests that all your custom dispatcher signals should go in a file called
signals.py
. However, that doesn't fully solve your problem, since you still need to import these somewhere, and the earlier they get imported the better.The model suggestion is a good one. Since you already defined everything in your
signals.py
file, it shouldn't take more than a line at the top of the the file. This is similar to the way theadmin.py
file is laid out (with class definitions at the top and the code for registering all the custom admin classes at the bottom), if you define your signals then connect them in the same file.Hope that helps! Ultimately it comes down to what you prefer.
每个应用程序中的 models.py 和 Signals.py 都是连接信号的推荐位置,但是,在我看来,它们并不是保持信号和处理程序调度的最佳解决方案。调度应该是 django 中发明信号和处理程序的原因。
我苦苦思索了很久,终于找到了解决办法。
在应用程序文件夹中创建一个连接器模块,
这样我们就可以:
在 app/connectors.py 中,我们定义了信号处理程序并连接它们。提供了一个示例:
然后在 models.py 中,我们在文件末尾添加以下行:
Everything did here。
这样,我们就可以将信号放入signals.py中,并将所有处理程序放入connectors.py中。模型和信号不会混乱。
希望它提供另一种解决方案。
models.py and signals.py in each app have been the recommended places to connect signals, however, they are not the best solution, in my opinion, to keep signals and handlers dispatched. Dispatching should be the reason signals and handlers invented in django.
I was struggling for long time, and finally we figured out the solution.
create a connector module in app folder
so we have:
in app/connectors.py, we defined signal handlers and connect them. An example is provided:
then in models.py, we add the following line in the end of the file:
Everything done here.
In this way, we can put signals in signals.py, and all the handlers in connectors.py. No mess in models and signals.
Hope it provides another solution.
关于 AppConfig 的小提醒。不要忘记设置:
Small reminder about
AppConfig
. Don't forget to set:我将它们保存在一个单独的文件
signals.py
中,在定义所有模型后的models.py
中。我导入它们并将模型连接到信号。Signals.py
models.py
这为我提供了逻辑分离,当然将它们保留在 models.py 中没有任何问题,但这种方式更易于管理。
希望这有帮助!
I keep them in a separate file
signals.py
, Inmodels.py
after all models are defined. I import them and connect models to signals.signals.py
models.py
This provides me logical separation, of course there is nothing wrong on keeping them in models.py , But it is more manageable this way.
Hope this helps!!