Django应用程序生命代码(如连接信号) [英] Django app initalization code (like connecting to signals)

查看:69
本文介绍了Django应用程序生命代码(如连接信号)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个地方来运行特定于应用程序的初始化代码(如连接到信号)。
当我将代码放到应用程序的 __ init __。py 模块时,我已经结束了模型的循环导入。

I need a place to run an initialization code that is application specific (like connecting to signals). When I put the code to __init__.py module of an application I've ended up with a circular import of the models.

是否有一种方法可以在框架设置和任何请求执行之前触发函数?

Is there a way to fire a function when the framework is setup and before any request is executed?

我使用相当旧的django版本96.6,但我也对当前版本的解决方案感兴趣。

I use quite old version of django 96.6, but I'm also interested in the solutions for the current version.

关于其他问题的重复:
这里问题与S.Lott在评论中提出的重复问题有什么不同?

Regarding the duplication of other questions: Here is how the question differ from the duplicates suggested by S.Lott in comments:

  • Correct place to put extra startup code in django? Django need to be fully initialized when the function is ran. So code in manage.py won't work.

我应该在Django框架中放置一次性操作?
函数初始化我的应用程序之间的连接。所以代码必须在实际处理请求的每个线程中运行。

Where should I place the one-time operation operation in the Django framework? The function initialize the connection between my applications. So the code must be ran in each thread that will actually handle the requests.

对当前解决方案的评论:
我无法使用网址,因为我的大部分应用没有任何网址暴露。他们只是听信号并在数据库中存储附加信息。

Comments to current solutions: I can't use urls as most of my apps don't have any urls exposed. They just listen to signals and store additional information in the database.

推荐答案

信号,具体来说,建议放在模型中.py的应用程序。

Signals, specifically, are recommended to be put in the models.py of your app.

尝试models.py或urls.py,让我们知道,如果你有任何运气。

Try models.py or urls.py and let us know if you have any luck.

这篇关于Django应用程序生命代码(如连接信号)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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