我正在尝试模块化我的django设置文件,以便更轻松地部署和管理我们的多个环境。
我已将其设置为按此顺序加载文件...
允许两个辅助文件更改先前文件中设置的值
我发现如果1使用正常
加载2和3from config.country_XX import *
我保留了在这些文件中所做的更改。
但是,1使用
加载2和3__import__()
或
importlib.import_module()
我所做的更改不会保留。
我更喜欢使用import_module,因为它允许我编写更清晰的代码
import_module('config.country_' + country)
而不是
if country = 'AA':
from config.country_AA import *
elif: country == 'BB'
from config.country_BB import *
...
这就是我所拥有的......让我知道你的想法。
settings.py
import os
import sys
from django.utils import importlib
DEVELOPMENT = True
DEBUG = False
USES_24_HOUR_TIME = True
country_config = 'config.country_us'
developer_config = 'config.developer_jack'
try:
#importlib.import_module(country_config)
from config.country_us import *
if DEVELOPMENT:
#importlib.import_module(developer_config)
from config.developer_jack import *
except ImportError:
pass
配置/ country_us.py
import sys
globals().update(vars(sys.modules['settings']))
USES_24_HOUR_TIME = False
配置/ developer_jack.py
import sys
globals().update(vars(sys.modules['settings']))
DEBUG = True