django应用程序的应用程序设置帮助程序。
django-app-settings的Python项目详细描述
django应用程序设置
django应用程序的应用程序设置帮助程序。
为什么还要另一个app settings应用? 因为其他的都不适合我的需要!
这个方法简单易用,并且可以使用单元测试覆盖设置。
安装
pip install django-app-settings
发展
运行所有测试:tox。见CONTRIBUTING。
快速使用
# Define your settings classimportappsettingsclassMySettings(appsettings.AppSettings):boolean_setting=appsettings.BooleanSetting(default=False)required_setting=appsettings.StringSetting(required=True)named_setting=appsettings.IntegerSetting(name='integer_setting')prefixed_setting=appsettings.ListSetting(prefix='my_app_')classMeta:setting_prefix='app_'# Related settings in settings.pyAPP_INTEGER_SETTING=-24MY_APP_PREFIXED_SETTING=[]# Instantiate your class wherever you need toappconf=MySettings()assertappconf.boolean_settingisFalse# True (default value)assertappconf.required_setting=='hello'# raises AttributeErrorassertappconf.named_setting<0# Trueassertappconf.prefixed_setting# False (empty list)# Values are cached to avoid perf issueswithoverride_settings(APP_REQUIRED_SETTING='hello',APP_INTEGER_SETTING=0):# ...but cache is cleaned on Django's setting_changed signalassertappconf.required_setting=='hello'# Trueassertappconf.named_setting<0# False# You can still access settings through the class itself (values not cached)print(MySettings.boolean_setting.get_value())# explicit callprint(MySettings.boolean_setting.value)# with property# Run type checking and required presence on all settings at onceMySettings.check()# raises Django's ImproperlyConfigured (missing required_setting)# MySettings.check() is best called in django.apps.AppConfig's ready method
您可以轻松地为更复杂的设置创建自己的设置类。
importreimportappsettingsfromdjango.core.exceptionsimportValidationErrorclassRegexSetting(appsettings.Setting):defvalidate(self,value):re_type=type(re.compile(r'^$'))ifnotisinstance(value,(re_type,str)):# Raise ValidationErrorraiseValidationError('Value must be a string or a compiled regex (use re.compile)')deftransform(self,value):# ensure it always returns a compiled regexifisinstance(value,str):value=re.compile(value)returnvalue
请查看文档以了解更高级的用法。
许可证
根据ISC许可证授权的软件。
更改日志
0.6.0(2019-08-27)
0.5.1(2019-05-23)
- 修复空参数的默认值。
0.5.0(2018-12-03)
- 与django表单字段类似,不赞成设置检查程序以支持验证器。
0.4.0(2018-07-25)
- 添加NestedSetting以便于管理嵌套设置。
0.3.0(2017-11-30)
从α状态到β状态。逻辑已经被改写了。
- AppSettings子类的实例现在将动态获取 从项目设置中设置值,并缓存它们。这允许使用 实例在代码和测试中的方式相同,没有性能损失。见 发布#16。
- 当django发送setting_changed信号(即 当使用TestCase或override_settings时。见第#16期。
- 设置main类现在接受callable作为默认值,两个新的 控制其行为的参数:call_default,它告诉 是否应该调用默认值(如果可以调用),以及 transform_default,它告诉是否应该转换默认值 以及transform方法。见第#17期。
- 设置类型检查程序现在具有自定义参数,如max_length, empty或key_type,可以直接通过设置传递 类作为关键字参数。查看文档以了解更多信息。
- 使用类继承更明确地重写了设置类 而不是难以调试的生成器。组合类型,如浮点列表或 已删除布尔集,以支持更灵活的列表、集合和元组。 现在接受可选参数item_type的类型。
- ImportedObjectSetting已重命名为ObjectSetting,现在 支持到任意嵌套级别的对象路径。以前,只是 支持到类或函数的对象路径,现在可以 给它一个类中一个常量的路径,它本身包含 在包内的模块中。只要最深的模块 可通过importlib.import_module和每个对象导入到 最后一个可以通过getattr方法获得。
非常感谢ziima分享了好的想法和想法!
0.2.5(2017-06-02)
- 添加六个依赖项(现在是必需的)。
- 将Int设置重命名为Integer,将Bool设置重命名为Boolean。
- 移除元类生成的getter和checker。
0.2.4(2017-05-02)
- 当设置默认为提供的默认值时,不会选中它们。
- 设置类收到与其类型相对应的更好的默认值。
0.2.3(2017-05-02)
- 将full_name属性添加到Setting类。
- 将required参数添加到Setting类(默认值False)。
0.2.2(2017-04-17)
- 在主模块中导入设置类以简化导入。
0.2.1(2017-04-17)
- 添加PositiveInt和PositiveFloat设置。
- 添加对django 1.11的支持。
- 实现基本设置类。
0.2.0(2017-04-17)
- 实现基本设置类。
- 固定依赖项。
- 将分发名称更改为app-settings。
0.1.0(2017-03-23)
- PYPI上的α释放。