django模型的版本文件字段
django-vff的Python项目详细描述
django vff
简介
这个包为django模型提供了一个文件字段,它将文件内容存储在vcs(版本控制系统)下。每当为特定模型实例更改此类型的字段时,新内容将作为新版本提交到存储库中。因此,对于每个vff字段和实例,存储库中将有一个文件。存储库将位于settings.VFF_REPO_ROOT,如果未设置,则位于django的settings.MEDIA_ROOT的vf_repo子目录。
使用可插入的后端,可以使用不同的vcs来管理存储库。该包仅提供一个现成的GIT后端。
安装
安装django vff,就像安装任何其他pypi包一样:
$ pip install django-vff
您不需要在django的INSTALLED_APPS中添加任何内容。
配置
您必须在django的settings.py中设置以下变量:
- VFF_BACKEND
- 指向后端类的虚线名称,例如"vff.git_backend.GitBackend"。此设置是必需的。
对于git后端:
- VFF_REPO_ROOT
- git存储库位置的绝对路径。在建立Django VFF之前,这个库可能存在,也可能不存在。
- VFF_REPO_PATH
- git存储库中到django vff保存其托管文件的目录的相对路径。
如果没有为git后端设置这两个设置,VFF_REPO_ROOT将假定值为os.path.join(settings.MEDIA_ROOT, 'vf_repo'),而VFF_REPO_PATH将假定值为''。
使用量
使用它就像使用django.db.models.FileField:
from django.db import models from vff.field import VersionedFileField class MyModel(models.Model): name = models.CharField('Name', max_length=128) content = VersionedFileField(name='content', verbose_name='file content')
一旦有了MyModel类的实例,就可以使用三种特殊方法来列出可用的版本、获取特定的版本以及获取版本之间的差异:
list revisions:
>>> revs = instance.content.list_revisions() >>> from pprint import pprint >>> pprint(revs) [{'author': u'John Smith', 'date': datetime.datetime(2011, 6, 16, 13, 25, 30), 'message': u'second version', 'versionid': 'a64ea785e195bbf4b3064e6701adbdbf4b5d13be'}, {'author': u'Martha Brown', 'date': datetime.datetime(2011, 6, 16, 8, 24, 36), 'message': u'first version', 'versionid': '048848a70205d0e18d836f403e2a02830492cbf9'}]get the string content of a specific revision:
>>> rev1_id = revs[-1]['versionid'] >>> instance.content.get_revision(rev1_id) u'These are the contents of the first version of the file'get the diff between two revisions:
>>> rev2_id = revs[-2]['versionid'] >>> print instance.content.get_diff(rev1_id, rev2_id) --- 048848a70205d0e18d836f403e2a02830492cbf9 +++ a64ea785e195bbf4b3064e6701adbdbf4b5d13be @@ -1,1 +1,1 @@ -These are the contents of the first version of the file +These are the contents of the second version of the file
保存和删除
目前,必须显式地保存和删除此字段。因此,例如,如果您有一个带有contentvff字段的模型实例,以及一个使用带有forms.FileField的编辑表单的视图,那么在验证表单之后,您必须执行如下操作:
name = instance.content.name content = form['content'].data username = request.user.username commit_msg = form['commit_msg'].data.encode('utf8') instance.content.save(name, content, username, commit_msg) instance.save()
同样,删除实例时,您将:
username = request.user.username commit_msg = u'entity removed' instance.content.delete(username, commit_msg) instance.delete()
在将来,如果有兴趣的话,包可以包含一个特殊的小部件,该小部件具有必要数据(提交消息等)的输入空间,以便保存和删除操作是透明的。
提供新的后端
要为django vff开发一个新的后端,必须将抽象基类vff.abcs.VFFBackend子类化。需要实现的方法记录在类的docstrings中。
更改
0.2b2(2012-01-25)
- Fix typo on README.txt
- Fix uncaught exception during initialization of GIT repository.
0.2b1(2012-01-24)
- The (git) repo can now be anywhere in the filesystem (not necessarily inside the MEDIA_ROOT), and django-vff can be told to keep its files in a subdirectory iof the repo.
0.1b4(2011-10-01)
- Better fix for error when deleting objects with no file in the repo.
0.1b3(2011-10-01)
- Do not fail deleting objects with no file in the repo.
0.1b2(2011-09-20)
- Remove the files from the repository when deleting objects.
- Change the signature of VersionedFieldFile’s save and delete so they are compatible with django’s FieldFile.
0.1b1(2011-09-02)
- Initial version which includes a VersionedFileField and a git backend.