Django可重用应用程序配置 [英] Django Reusable Application Configuration

查看:159
本文介绍了Django可重用应用程序配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些连接到数据库的Django中间件代码。我想把这个中间件变成一个可重用的应用程序(app),所以我可以将它打包成很多其他的项目,而不需要复制粘贴。



我不明白可重用的应用程序应该配置自己的位置。由于它用于重新分配,我无法自己编写中央的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屋!

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