boot.settings module¶
- class boot.settings.SettingConfig(source: Config | dict[str, Any], dict_prefix: str = '')[源代码]¶
基类:
Config
Configurables in django framework w.r.t the project.
For example, login url is not configurable and should be hard-coded.
- db_host = 'mysql'¶
- db_name = 'yppf'¶
- db_password = 'secret'¶
- db_port¶
延迟加载的配置项
在Config类中作为属性定义,如:
class AppConfig(Config): # 由语言服务器自动推断类型 value = LazySetting('value', default=0) op1 = LazySetting('op1', int) op2 = LazySetting('op2', int, default=0) # 也可以使用type指定类型,写法建议参考`LazySetting.checkable_type`的文档 assert_d = LazySetting('value/dict', type=dict) i_or_s = LazySetting('value/i_or_s', type=(int, str)) assert_ls = LazySetting('value/ls', type=list[str]) tuple = LazySetting('value/tuple', type=tuple[int, str])
上述代码在访问时会自动计算并缓存结果:
config = AppConfig() # 自动推断的类型 config.value: int config.op1: int | None config.op2: int # 用type指定的类型 config.assert_d: dict config.i_or_s: int | str # 检查是否为int或str list[str], tuple[int, str] # 检查是否为列表,元组等,不检查元素类型
- 抛出:
ImproperlyConfigured -- 配置最终值不匹配期望类型
- db_user = 'root'¶
- secret_key = 'k+8az5x&aq_!*@%v17(ptpeo@gp2$u-uc30^fze3u_+rqhb#@9'¶
- static_dir = '/workspace'¶