Django可重用应用程序配置 [英] Django Reusable Application Configuration
问题描述
我不明白可重用的应用程序应该配置自己的位置。由于它用于重新分配,我无法自己编写中央的settings.py。看看Django文档,我看到有settings.configure,但它似乎取代了整个配置,而不是让我将一个新的数据库拼接到DATABASES中。
什么是正确的方式给我可重用的中间件应用程序配置自己的数据库连接的能力?我不希望它干扰要安装它的应用程序的数据库。谢谢。
您可以按照Django调试工具栏的方法。它包含一个应用程序配置类,并覆盖 准备好
方法。
您可以看到代码 here 。
I have some Django middleware code that connects to a database. I want to turn the middleware into a reusable application ("app") so I can package it for distribution into many other projects, without needing to copy-and-paste.
I don't understand where a reusable application is supposed to configure itself. Since it's intended for redistribution I don't have the ability to write the central settings.py myself. Looking at the Django documentation I see there's settings.configure but it appears to replace the entire configuration, instead of letting me "splice" a new database into DATABASES.
What's the right way to give my reusable middleware app the ability to configure its own database connection? I don't want it to interfere with the databases of applications where I'll be installing it. Thanks.
You could follow the approach of Django debug toolbar. It includes an app config class, and overrides the settings in the ready
method.
You can see the code here.
这篇关于Django可重用应用程序配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!