django的可重用单例模型
django-singleton的Python项目详细描述
#django singleton
django singleton是thomas ashelford的[django singleton](https://github.com/tttallis/django-singletons)的分支。
我派生了他的代码以包含django 1.4兼容性,并删除了管理员中的delete按钮(见下面的内容)。我不得不重命名存储库,以便将其提交到python包索引。
现在包括django 1.6支持,同时保持向后兼容。
[克里斯·丘奇叉子上的代码](https://github.com/ninemoreminutes/django-singletons/commit/9b231666b9027d3bd1159f3db8bce34701193bdd)-我只是综合所有这些…
##用托马斯的话说
我一直发现自己在使用这段简单的代码,所以我认为我应该开源它,尽管它只不过是一个片段。
singletonmodel是一个只有一个记录的django模型。不能使用管理员创建新实例,或者删除现有实例。
有些人可能会说,单例模型是使用关系数据库的一种低效方法,但实际上这没什么大不了的——大多数网站都有一些重要的一次性内容(如主页),而且单例模型很好地映射到内容编辑器通常的思想方式。
我怀疑我已经从其他人(可能是前同事http://github.com/jphalip/)那里获得了一些代码,但是一个快速的google并没有出现类似的东西。就在这里。希望你觉得有用。
###安装
pip install django-singleton
要使自定义管理模板正常工作,您需要将“singleton\u models”添加到已安装的应用程序中
###示例用法
在models.py中
from singleton_models.models import SingletonModel
- class HomePage(SingletonModel):
welcome = models.TextField()
- def __unicode__(self):
- return u”The Home Page” # something like this will make admin message strings more coherent
- class Meta:
- verbose_name = “Home Page” # once again this will make sure your admin UI doesn’t have illogical text verbose_name_plural = “Home Page”
在admin.py中
from singleton_models.admin import SingletonModelAdmin from models import HomePage
admin.site.register(HomePage, SingletonModelAdmin)