简单的django可重用应用程序,用于将项目设置存储在数据库中。
django-settings的Python项目详细描述
Django可重用应用程序,用于在数据库中存储全局项目设置。
我所说的项目设置是指管理邮件,一些默认值如 根据其类型验证默认值。 从1.3版开始,您可以注册自己的设置值。
支持的版本
- python:2.6、2.7(python 2.6需要importlib)
- django:1.3、1.4、1.5
API
重要提示:在版本1.3中进行了更改,旧的api仍然可以工作,但是缓存不能使用它。
importdjango_settings# getting values# this will raise django_settings.models.Setting.DoesNotExist# exception if value not exists# if value is not in cache it will be cacheddjango_settings.get('post_limit')# if you not sure value exists you can pass "default" parameter,# at this point default is NOT cacheddjango_settings.get('post_limit',default=10)# set values - cache is updated for "get" and "exists" method# values are validated using setting_value model clean_fields methoddjango_settings.set('Email','admin_email','admin@admin.com')# If you want to avoid validation, do this:django_settings.set('Email','admin_email','admin@admin.com',validate=False)# checking if value existsdjango_settings.exists('admin_email')# getting all values as a dictdjango_settings.all()
安装和设置
- 使用pip安装:
$> pip install django-settings
- 将“django_设置”添加到已安装的_应用程序中
INSTALLED_APPS=('django.contrib.contenttypes',# contenttypes framework is required# ...'django_settings',# ...)
- 如果要添加自己的设置模型,请将其添加到 应用程序模型文件,并使用django_设置api注册它们:
# <project>/<app>/models.pyfromdjango.dbimportmodels# ... your application modelsimportdjango_settingsclassText(django_settings.db.Model):value=models.TextField()classMeta:abstract=True# it's IMPORTANT - it need to be abstractdjango_settings.register(Text)
记住将模型定义为抽象的,这很重要,因为django 处理模型类。
可以通过project settings.py文件设置一些默认值。 只有在数据库中尚未存在这些设置时,才会设置这些设置。
DJANGO_SETTINGS={'application_limit':('Integer',2),'admin_email':('String','admin@mail.com'),}
要将django_设置导入数据库,请使用命令:
./manage.pysettings_initialize
默认django设置超时设置为1天,但可以更改 在项目设置中:
DJANGO_SETTINGS_TIMEOUT=60*60*10# 10 hours
timeout让我们为每个 设置。在给定的时间值过期后 将重新计算(在您要求给定的 设置)。由于Django的默认缓存超时(5分钟)而引入: https://docs.djangoproject.com/en/dev/topics/cache/#cache-arguments
设置类型
内置设置类型:email、integer、string、positiveinteger
管理
您可以通过管理界面操作设置。
更改日志
1.3.12-已修复同步后数据库设置初始化
- initial signal moved to models
1.3-11-几个错误修复程序
- “Clear cache for settings” admin action exception fixed [#12]
- admin setting edition cache update bug fixed
- “syncdb” signal callback is now fixed so it won’t “reinit” settings on every syncdb [#14]
1.3-8-django_settings_timeout fix
- it’s now cofigurable through project settings
1.3-7-一些改进和错误修复
- “all” function added
- admin setting add/edit callback: “DataAPI._set_cache_for” bug fix.
- settings timeout customization added (default to 1 day)
1.3-4-setup.py错误修复
1.3-3 beta-python&django各种版本的兼容性更改
1.3-2 beta-几个错误修复,包括缓存Unicode密钥处理,添加测试
1.3-1测试版-管理员呈现更改表单修复程序
1.3 beta版-自1.0版以来已进行了一些改进
- setting name need to be unique now (backward incompatiblity)
- from now you can extend settings with your own types using django_settings.register function
- new api with caching mechanism introduced
- admin interface has been improved, action to clear cache keys only used by the package added
Some tests has been added for core functionality.
向后不兼容的更改
django_settings.models.Setting name need to be unique now, however ver 1.3 still allows it to not to be unique. Just set DJANGO_SETTINGS_UNIQUE_NAMES application setting to False (True is by default).