可插拔应用程序的Django默认设置约定? [英] Django default settings convention for pluggable app?
问题描述
如果在 settings.py
?中未定义应用程式中的预设设定,如何处理预设设定的方式是什么?
我目前在应用中放置了一个 default_settings
文件,我已经考虑过几个选项。我倾向于第一个选项,但可能会有陷阱我不知道使用全局()
我大多看到应用程序在使用该设置的文件顶部有一个 FOO = getattr(settings,'FOO',False)
,但我认为
1:将设置置于a function and iterate over locals / set globals
def setup_defaults():
FOO ='bar '
for key,value in locals()。items():
globals()[key] = getattr(settings,key,value)
setup_defaults $ b
优点:
缺点:
- 不适用于使用全局()且不知道任何含义
2:写入 getattr(设置,'MY_SETTING',default_settings.MY_SETTING)
>
优点:$ b $ b - 很清楚。
缺点: - 重复b
$ b
3:始终将设置定义为 FOO = getattr(settings,'FOO','...设置此处...')
优点:$ b $ b - 默认值总是被覆盖
缺点:
- 重复(必须定义var两次 - 一次为字符串形式,一次为var) / li>
- 设置不可读,因为它现在是第三个参数
4:为 get_or_default(设置)
创建效用函数
优点:
- 简单
- 不必重复字符串表示的设置
缺点:
- >
5:创建设置类
$ b
class Settings(object):
FOO ='bar'
def __init __(self):
#过滤掉
#self .__ dict __。items()/与django.conf.settings比较的startswith('__')?
my_settings =设置()
缺点:
- 无法从foo.bar.my_settings import FOO(实际上,这是一个可怕的交易破坏!)
我很想听到反馈。
我认为创建 settings.py
您的应用程序的包,其中您定义您的设置,例如:
从django.conf导入设置
FOO = getattr设置,'FOO',default_value)
在您的应用程序中, code>设置模块:
来自myapp.settings import *
def print_foo():
print FOO
但我认为大家都同意Django缺乏一个更好的通用架构!如果您正在寻找一种更复杂的方法来处理此问题,则有一些第三方应用程序,例如 django-appconf ,但是这是你的决定,如果你想为你的应用程序或者不再引入一个依赖关系。
What's a djangonautic way of handling default settings in an app if one isn't defined in settings.py
?
I've currently placed a default_settings
file in the app and I've considered a few options. I'm leaning towards the first option, but there may be pitfalls I'm not aware of in using globals()
I've mostly seen apps do a FOO = getattr(settings, 'FOO', False)
at the top of the file that uses the setting but I think there are readability/repetition problems with this approach if the values / names are long.
1: Place settings in a function and iterate over locals / set globals
def setup_defaults():
FOO = 'bar'
for key, value in locals().items():
globals()[key] = getattr(settings, key, value)
setup_defaults()
Pros:
- Only have to write var name once to pull default of same name from django settings.
Cons:
- Not used to using globals() and don't know of any implications
2: Write getattr(settings, 'MY_SETTING', default_settings.MY_SETTING)
every call
Pros: - Very clear.
Cons: - Repetitive
3: Always define settings as FOO = getattr(settings, 'FOO', '...setting here...')
Pros: - Defaults are always overridden
Cons:
- Repetitive (must define var twice - once in string form, once in var)
- Setting is not as readable since it's now the third argument
4: Create utility function to get_or_default(setting)
Pros:
- Simple
- Don't have to repeat string representation of setting
Cons:
- Have to call it
5: Create a settings class
class Settings(object):
FOO = 'bar'
def __init__(self):
# filter out the startswith('__') of
# self.__dict__.items() / compare to django.conf.settings?
my_settings = Settings()
Cons:
- Can't do from foo.bar.my_settings import FOO (actually, that's a terrible deal breaker!)
I'd love to hear feedback.
I think it's quite common to create a settings.py
in your app's package, where you define your settings like this:
from django.conf import settings
FOO = getattr(settings, 'FOO', "default_value")
In your app you can import them from your app's settings
module:
from myapp.settings import *
def print_foo():
print FOO
But I think everybody agrees that Django is lacking a better generic architecture for this! If you're looking for a more sophisticated way to handle this, there are some third party apps for this like django-appconf, but it's your decision if you want to introduce one more dependency for your app or not!
这篇关于可插拔应用程序的Django默认设置约定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!