一个Django应用程序,将应用程序设置保存在数据库中作为类。
django-db-settings的Python项目详细描述
django-db-settings是一个django应用程序,用于将配置保存在可缓存的db对象中,这些对象很容易定义为类。在
快速入门
使用pip安装django-db-settings:
在pip install django-db-settings
将“设置”添加到已安装的应用程序设置中,如下所示:
INSTALLED_APPS = [ ... 'settings', ]
在在项目中包含设置URLconf网址.py像这样:
^{pr2}$ 在运行python manage.py migrate创建设置模型。在
在启动开发服务器并访问http://127.0.0.1:8000/admin/以设置应用程序设置(需要启用管理应用程序)。在
在访问http://127.0.0.1:8000/settings/?setting=YOUR_SETTING获取与该特定设置(JSON)相关的对象。在
在在“值模型更改列表”页面中找到“刷新设置”按钮。此项目使用基于TTL的缓存,可以通过添加以下设置来配置:
在- SETTINGS_CACHE_MAXSIZE: To set the maximum size of total items in the cache. By default set to 100.
- SETTINGS_CACHE_TTL: To set the Time To Live of the cache items. By default set to 3600 seconds (1 hour).
基本用途
django数据库设置将您的设置保存在一个灵活的模型中。要创建设置,请执行以下步骤:
- 在模型Setting中创建一个设置,它的行为类似于将一组设置分组的类。e、 g.,product type
- 通过在模型Field中添加这些属性来定义Setting的属性。您可以将它们分别设置为public或private。e、 g.,title,description,code。在
- 在模型Instance中添加Setting实例。它们将充当设置类的对象。e、 g.,saving,credit card,loan。在
- 最后,在模型Value中为由为Setting定义的字段指导的每个实例添加值。e、 例如,对于saving实例,值为title:储蓄帐户,description:节省金钱,code:S001。在
在添加所有值之后,您将能够通过转到http://127.0.0.1:8000/settings/?setting=product%20type来检索所有这些字段是公共的。它返回一个JSON对象:
{“saving”: {“title”: “Saving account”, “description”: “Save your money with us!”}, “credit card”: {“title”: “Credit Card”, “description”: “Get the best from our Credit Card”}, “loan”: {“title”: “Loan”, “description”: “We loan you the money you need”}}
提示:通过在模块settings.business中调用方法get_setting,可以在内部使用相同的设置:
>>> get_setting('product type') {'saving': {'title': 'Saving account', 'description': 'Save your money with us!'}, 'credit card': {'title': 'Credit Card', 'description': 'Get the best from our Credit Card'}, 'loan': {'title': 'Loan', 'description': 'We loan you the money you need'}}
在内部,您可以设置参数include_non_public=True,以检索私有字段:
>>> get_setting('product type', include_non_public=True) {'saving': {'title': 'Saving account', 'description': 'Save your money with us!', 'code': 'S-001'}, 'credit card': {'title': 'Credit Card', 'code': 'C-001', 'description': 'Get the best from our Credit Card'}, 'loan': {'title': 'Loan', 'description': 'We loan you the money you need', 'code': 'L-001'}}
从同一个包调用方法clear_settings_cache,以清除缓存并刷新全局设置。在
- 项目
标签: