简体   繁体   English

每个应用程序的 Django 设置 - 最佳实践?

[英]django settings per application - best practice?

this is somewhat related to this question这与这个问题有些相关
Why is django's settings object a LazyObject? 为什么 Django 的设置对象是一个 LazyObject?

In my django project i have several applications.在我的 Django 项目中,我有几个应用程序。 Each application can have its own non-trivial settings file.每个应用程序都可以有自己的重要设置文件。

proj/
    proj/
         settings.py
    app/
         settings.py
         views.py

What is the general best practice here?这里的一般最佳实践是什么?
should app/settings.py do app/settings.py 应该做

from django.conf import settings
APP_SETTING= lambda: settings.getattr('APP_SETTING', 'custom_value')
PROJ_SETTING= lambda: settings.PROJ_SETTING

and then in app/views.py do然后在 app/views.py 中做

import .settings 
X = settings.APP_SETTING
Y = settings.PROJ_SETTING

or should I be modifying the django lazy settings object in app/settings.py as per the django coding style?还是我应该按照 django 编码风格修改 app/settings.py 中的 django 延迟设置对象?

from django.conf import settings
# not even sure how I would check for a default value that was specified in proj/settings.py
settings.configure(APP_SETTING='custom_value')

and then each app/views.py just consumes proj/settings.py via django.conf settings?然后每个 app/views.py 只是通过 django.conf 设置消耗 proj/settings.py?

from django.conf import settings
X = settings.APP_SETTING
Y = settings.PROJ_SETTING

There are obviously quite a few other permutations but I think my intent is clear.显然还有很多其他排列,但我认为我的意图很明确。
Thanks in advance.提前致谢。

The simplest solution is to use the getattr(settings, 'MY_SETTING', 'my_default') trick that you mention youself.最简单的解决方案是使用您自己提到的getattr(settings, 'MY_SETTING', 'my_default')技巧。 It can become a bit tedious to have to do this in multiple places, though.但是,必须在多个地方执行此操作可能会变得有些乏味。

Extra recommendation: use a per-app prefix like MYAPP_MY_SETTING .额外建议:使用每个应用程序前缀,如MYAPP_MY_SETTING

There is a django app, however, that gets rid of the getattr and that handles the prefix for you.但是,有一个 django 应用程序可以摆脱getattr并为您处理前缀。 See http://django-appconf.readthedocs.org/en/latest/请参阅http://django-appconf.readthedocs.org/en/latest/

Normally you create a conf.py per app with contents like this:通常,您为每个应用程序创建一个conf.py ,其内容如下:

from django.conf import settings
from appconf import AppConf

class MyAppConf(AppConf):
    SETTING_1 = "one"
    SETTING_2 = (
        "two",
    )

And in your code:在你的代码中:

from myapp.conf import settings

def my_view(request):
    return settings.MYAPP_SETTINGS_1  # Note the handy prefix

Should you need to customize the setting in your site, a regular entry in your site's settings.py is all you need to do:如果您需要自定义站点中的settings.py ,只需在站点的settings.py输入常规条目即可:

MYAPP_SETTINGS_1 = "four, four I say"

Since Django 1.7 there is a django-based structure for app-oriented configurations!从 Django 1.7 开始,有一个基于 django 的结构用于面向应用程序的配置! You could find descriptive solution here您可以在此处找到描述性解决方案

In this new structure, conventionally you could have an apps.py file in your applications' folder which are embeded in project, something like this:在这个新结构中,通常你可以在你的应用程序文件夹中有一个apps.py文件嵌入到项目中,如下所示:

proj/
    proj/
         settings.py
    app1/
        apps.py
        views.py
    app2/
        apps.py
        views.py

app1/apps.py file could include something like this: app1/apps.py文件可能包含如下内容:

from django.apps import AppConfig


class App1Config(AppConfig):
    # typical systemic configurations
    name = 'app1'
    verbose_name = 'First App'

    # your desired configurations
    OPTION_A = 'default_value'
    APP_NAMESPACE = 'APP'
    APP_OPTION_B = 4

you could have app2/apps.py something different like this:你可以让app2/apps.py像这样不同:

from django.apps import AppConfig


class App2Config(AppConfig):
    # typical systemic configurations
    name = 'app2'
    verbose_name = 'Second App'

    # your desired configurations
    OTHER_CONFIGURATION = 'default_value'
    OPTION_C = 5

and so etc for other apps.py s in you Django Application folder. Django Application 文件夹中的其他apps.py等等。

It's important that you should import applications you included apps.py in, as follows:重要的是您应该导入包含apps.py应用程序,如下所示:

# proj/settings.py

INSTALLED_APPS = [
    'app1.apps.App1Config',
    'app2.apps.App2Config',
    # ...
]

‌Now, You could access desired app-based configuration someway like this: ‌现在,您可以像这样访问所需的基于应用程序的配置:

from django.apps import apps
apps.get_app_config('app1').OPTION_A

Not sure about best practices but I don't have any problems with following style:不确定最佳实践,但我对以下风格没有任何问题:

proj/settings.py项目/设置.py

OPTION_A = 'value'

# or with namespace
APP_NAMESPACE = 'APP'
APP_OPTION_B = 4

app/settings.py应用程序/settings.py

from django.conf import settings
from django.utils.functional import SimpleLazyObject

OPTION_A = getattr(settings, 'OPTION_A', 'default_value')

# or with namespace
NAMESPACE = getattr(settings, APP_NAMESPACE, 'APP')
OPTION_B = getattr(settings, '_'.join([NAMESPACE, 'OPTION_B']), 'default_value')
OPTION_C = getattr(settings, '_'.join([NAMESPACE, 'OPTION_C']), None)
if OPTION_C is None:
    raise ImproperlyConfigured('...')

# lazy option with long initialization
OPTION_D = SimpleLazyObject(lambda: open('file.txt').read())

app/views.py应用程序/视图.py

from .settings import OPTION_A, OPTION_B
# or
from . import settings as app_settings
app_settings.OPTION_C
app_settings.OPTION_D  # initialized on access

My site is just starting and I want a the simplest but flexible configuration solution.我的网站刚刚开始,我想要一个最简单但灵活的配置解决方案。 That's what I came across with.这就是我遇到的。

# in the end of the site's settings.py
. . .
# Custom settings
MYAPP_CONFIG_FILE = "/home/user/config/myapp_config.ini"

In my application's models.py :在我的应用程序的models.py

from django.conf import settings
import configparser

config = configparser.ConfigParser()
config.read(settings.MYAPP_CONFIG_FILE, encoding='utf_8')

This config parser is described here .此配置解析器在此处描述。 It's convenient enough but definitely not the only option.它足够方便,但绝对不是唯一的选择。

you can use django-zero-settings , it lets you define your defaults and a key for your user settings, then it will handle user overrides too.您可以使用django-zero-settings ,它允许您定义默认值和用户设置的key ,然后它也将处理用户覆盖。

it also auto imports your settings, has the ability to handle removed settings, and can pre-check settings too.它还可以自动导入您的设置,能够处理已删除的设置,还可以预先检查设置。

as an example, define your settings:例如,定义您的设置:

from zero_settings import ZeroSettings

app_settings = ZeroSettings(
    key="APP",
    defaults={
        "TOKEN": "token"
    },
)

then you can use it like:那么你可以像这样使用它:

from app.settings import app_settings

print(app_settings.TOKEN)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM