一个Django应用程序,将应用程序设置保存在数据库中作为类。

django-db-settings的Python项目详细描述


django-db-settings是一个django应用程序,用于将配置保存在可缓存的db对象中,这些对象很容易定义为类。在

快速入门

  1. 使用pip安装django-db-settings

    pip install django-db-settings

  2. 将“设置”添加到已安装的应用程序设置中,如下所示:

    INSTALLED_APPS = [
        ...
        'settings',
    ]
    
  3. 在项目中包含设置URLconf网址.py像这样:

    ^{pr2}$ 在
  4. 运行python manage.py migrate创建设置模型。在

  5. 启动开发服务器并访问http://127.0.0.1:8000/admin/以设置应用程序设置(需要启用管理应用程序)。在

  6. 访问http://127.0.0.1:8000/settings/?setting=YOUR_SETTING获取与该特定设置(JSON)相关的对象。在

  7. 在“值模型更改列表”页面中找到“刷新设置”按钮。此项目使用基于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数据库设置将您的设置保存在一个灵活的模型中。要创建设置,请执行以下步骤:

  1. 在模型Setting中创建一个设置,它的行为类似于将一组设置分组的类。e、 g.,product type
  2. 通过在模型Field中添加这些属性来定义Setting的属性。您可以将它们分别设置为public或private。e、 g.,titledescriptioncode。在
  3. 在模型Instance中添加Setting实例。它们将充当设置类的对象。e、 g.,savingcredit cardloan。在
  4. 最后,在模型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,以清除缓存并刷新全局设置。在

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java收回EhCache中的所有元素   java创建带有箭头的组合框,以在其中增加值   在Java中使用JsonPath解析JSON中的数组   java如何在应用程序类不可见的模块中获取上下文?   exoplayer中的java Recyclerview不起作用   java MS Access无法打开更多表   从xmlschema到java的unicode拉丁脚本子集的正则表达式   在spring sts模型中,java时间戳格式必须为yyyymmdd hh:mm:ss   java将XPath转换为Jsoup的CssSelector   java Solr运算符类似于SQL中的类反向运算符   java使第三方类不可变   java跳过自定义卡片堆栈视图的动画   java如何修复“使用Spring AOP,我想更改返回值,但返回类不是我的返回方法”   java使用正则表达式解析字符串   java泛型与遗留代码的兼容性为什么foreach在运行时失败,而迭代器工作正常   hibernate如何使用java持久性重试锁定等待超时?