一个Django应用程序,它提供了一个singleton模型,因此您可以将设置存储在数据库中。
django-valerie的Python项目详细描述
一个Django应用程序,它提供了一个singleton模型,以便您可以存储您的设置 在数据库中。在
支持Python3.6-3.8或更高版本以及Django 2.2-3.1。在
快速启动
使用pip安装应用程序:
pip install django-valerie
将其添加到已安装的应用程序中:
^{pr2}$为您的设置创建模型,例如:
# models.py from django.db import models from valerie.models import Settings class MySettings(Settings): site = models.CharField(max_length=50) logo = models.FileField(upload_to="files") comments = models.BooleanField() def get_initial(cls): initial = super().get_initial() initial["site"] = "My Awesome Site" initial["comments"] = True return initial
现在向Django管理员注册设置:
from django.contrib import admin from valerie.admin import SettingsAdmin from .models import MySettings @admin.register(MySettings) class MySettingsAdmin(SettingsAdmin): pass
使用默认值将记录按需添加到数据库中,因此 您可以立即使用代码中的设置,而无需使用 执行任何迁移:
settings = MySettings.fetch()
或在模板中,提供<;app>;<;model>;路径:
{% load valerie_tags %} {% valerie_settings "myapp.MySettings" as settings %}
现在可以将控制权交给站点管理员来更新值。在
缓存
如果启用了缓存,则有三个设置可控制 以及settings对象的缓存位置和缓存时间:
SETTINGS_CACHE_NAME = 'default' SETTINGS_CACHE_TIMEOUT = 60 * 60 # 1 hour SETTINGS_CACHE_PREFIX = "settings"
默认设置将设置存储在default缓存中1小时(看起来 合理的,因为它们的变化应该相对缓慢)。在
每当访问设置对象和缓存时,都会刷新缓存 条目已过期。每次设置对象 保存的。在
这些设置是在settings类上定义的,但您可以覆盖部分或全部 在Django设置中。在
合理的违约
设置模型中每个必需字段的默认值可以是 在Django设置中选择性地覆盖设置默认值。这是一个 字典,以便它可以包含每个设置类的条目:
SETTINGS_DEFAULTS = { "MySettings": { "site": "My New Awesome Site", "comments": False, } }
这使得在你发布的应用程序中很容易使用django valerie 可以根据需要覆盖合理的默认值。在
演示网站
如果从存储库中签出代码,则项目包含一个演示 该网站包含一个示例应用程序,其中包含设置的具体子类 这样你就可以看到django valerie是如何工作的。在
就这样吧
该项目有一个Makefile,它包含许多目标来支持 开发过程。最有用的可能是运行测试的测试 以及runserver来运行演示站点以显示Django管理站点。在那里 也是一组管理发布过程的目标。在
您可以通过在命令行上运行make来阅读简要说明:
Please use `make <target>' where <target> is one of: @echo "" @echo " help to show this list" @echo " clean-build to clean the files and directories generated by previous builds" @echo " clean-docs to clean the generated HTML documentation" @echo " clean-tests to clean the directories created during testing" @echo " clean-coverage to clean the test coverage data and reports" @echo " clean-venv to clean the virtualenv" @echo " clean to clean everything EXCEPT the virtualenv" @echo @echo " build to build the package" @echo " checks to run quality code checks" @echo " coverage to measure the test coverage" @echo " docs to build the HTML documentation" @echo " major to update the version number for a major release, e.g. 2.1 to 3.0" @echo " messages to run the makemessages and compilemessages management commands" @echo " migrate to run migrate management command" @echo " migrations to run makemigrations management command" @echo " minor to update the version number for a minor release, e.g. 2.1 to 2.2" @echo " patch to update the version number for a patch release, e.g. 2.1.1 to 2.1.2" @echo " runserver to run the Django demo site" @echo " test to run the tests during development" @echo " test-all to run the tests for all the supported environments" @echo " upload to upload a release to PyPI repository" @echo " venv to create the virtualenv and install dependencies"
类似于
- 项目
标签: