Django在“完全加载”之后如何运行任意代码 [英] How To Run Arbitrary Code After Django is "Fully Loaded"
问题描述
更具体地说,我需要做一些例如 Signal.disconnect()
一些由我的第三方库默认设置的Django信号, connect
我自己的信号,我需要做一些猴子补丁,从另一个图书馆向一些Django模型添加便利功能。
我已经在我的Django应用程序的 __ init __。py
文件中执行此操作,这似乎可以适用于猴子修补,但不适用于我的信号断开连接。这个问题似乎是一个时间 - 无论什么原因,第三方图书馆在尝试之后总是似乎称之为
它。 Signal.connect()
Signal.disconnect()
所以两个问题:
根据我的 INSTALLED_APPS
订单的任何保证,当我的应用程序的 __ init __。py
模块加载时的顺序? / p>
是否有适当的地方放置需要在 后运行 的逻辑Django应用程序已经完全加载到内存中?
在Django 1.7应用程序可以实现ready()方法: https://docs.djangoproject.com/en/dev/ref/applications/#django.apps.AppConfig。准备好
I need to perform some fairly simple tasks after my Django environment has been "fully loaded".
More specifically I need to do things like Signal.disconnect()
some Django Signals that are setup by my third party library by default and connect
my own Signals and I need to do some "monkey patching" to add convenience functions to some Django models from another library.
I've been doing this stuff in my Django app's __init__.py
file, which seems to work fine for the monkey patching, but doesn't work for my Signal disconnecting. The problem appears to be one of timing--for whatever reason the Third Party Library always seems to call its Signal.connect()
after I try to Signal.disconnect()
it.
So two questions:
Do I have any guarantee based on the order of my INSTALLED_APPS
the order of when my app's __init__.py
module is loaded?
Is there a proper place to put logic that needs to run after Django apps have been fully loaded into memory?
In Django 1.7 Apps can implement the ready() method: https://docs.djangoproject.com/en/dev/ref/applications/#django.apps.AppConfig.ready
这篇关于Django在“完全加载”之后如何运行任意代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!