如何为自定义 Django 设置定义默认值
您可以添加的 Django 文档提及您自己的设置到django.conf.settings
。因此,如果我的项目的 settings.py
定义了,
APPLES = 1
我可以在该项目的应用程序中使用 settings.APPLES
访问它。
但是,如果我的 settings.py
没有定义该值,则访问 settings.APPLES
显然将无法工作。如果 settings.py
中没有显式设置,是否有某种方法可以为 APPLES
定义默认值?
我最好在需要设置的模块/包中定义默认值。
The Django documentation mentions that you can add your own settings to django.conf.settings
. So if my project's settings.py
defines
APPLES = 1
I can access that with settings.APPLES
in my apps in that project.
But if my settings.py
doesn't define that value, accessing settings.APPLES
obviously won't work. Is there some way to define a default value for APPLES
that is used if there is no explicit setting in settings.py
?
I'd like best to define the default value in the module/package that requires the setting.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
在我的应用程序中,我有一个单独的 settings.py 文件。在该文件中,我有一个 get() 函数,它在项目的 settings.py 文件中进行查找,如果找不到,则返回默认值。
然后,在我需要访问 APPLES 的地方,我有:
这允许在项目 settings.py 中进行覆盖,getattr 将首先检查那里,如果找到该属性,则返回该值,或者使用应用程序设置文件中定义的默认值。
In my apps, I have a seperate settings.py file. In that file I have a get() function that does a look up in the projects settings.py file and if not found returns the default value.
Then where I need to access APPLES I have:
This allows an override in the projects settings.py, getattr will check there first and return the value if the attribute is found or use the default defined in your apps settings file.
怎么样:
How about just:
这里有两个解决方案。对于这两种情况,您都可以在应用程序中设置
settings.py
文件并用默认值填充它们。为单个应用程序配置默认值
在代码中使用
from MYAPP import settings
而不是from django.conf import settings
。编辑
YOURAPP/__init__.py
:为整个项目配置默认值
在您的项目中使用
from MYPROJECT import settings
而不是from django.conf import settings
代码。编辑 MYPROJECT/MYPROJECT/__init__.py
此解决方案可能看起来更容易安装,但它不能保证返回良好的默认值。如果多个应用程序在其 settings.py 中声明相同的变量,您无法确定哪一个应用程序将返回您要求的默认值。
Here are two solutions. For both you can set
settings.py
files in your applications and fill them with default values.Configure default value for a single application
Use
from MYAPP import settings
instead offrom django.conf import settings
in your code.Edit
YOURAPP/__init__.py
:Configure default values for a whole project
Use
from MYPROJECT import settings
instead offrom django.conf import settings
in your code.Edit
MYPROJECT/MYPROJECT/__init__.py
This solution may seem more easier to install, but it does not guarantee that the good default value will be returned. If several applications declare the same variable in their settings.py, you can not be sure which one will return the default value you asked.
从迈克的回答开始,我现在将默认设置处理包装到一个具有易于使用的界面的类中。
帮助程序模块:
用法:
这将打印 django.conf.settings 中的值,如果未在那里设置,则打印默认值。此
settings
对象还可用于访问所有标准设置值。Starting from Mike's answer, I now wrapped the default setting handling into a class with easy to use interface.
Helper module:
Usage:
This prints the value from
django.conf.settings
, or the default if it isn't set there. Thissettings
object can also be used to access all the standard setting values.我最近遇到了同样的问题,并创建了一个 Django 应用程序,专门用于这种情况。它允许您定义某些设置的默认值。然后,它首先检查全局设置文件中是否设置了该设置。如果没有,它将返回默认值。
我将其扩展为还允许对默认值进行某些类型检查或预先处理(例如,可以在加载时将点分类路径转换为类本身)
该应用程序可以在以下位置找到: https://pypi.python.org/pypi?name=django-pluggableappsettings&version=0.2.0&:action=display
I recently had the same problem and created a Django app that is designed to be used for exactly such a case. It allows you to define default values for certain settings. It then first checks whether the setting is set in the global settings file. If not, it will return the default value.
I've extended it to also allow for some type checking or pre handling of the default value (e.g. a dotted class path can be converted to the class itself on load)
The app can be found at: https://pypi.python.org/pypi?name=django-pluggableappsettings&version=0.2.0&:action=display