Django:模型实例历史 [英] Django : Model Instance history
问题描述
在我的项目中,我必须处理上传的文件,我想要有每个文件版本的历史记录。并在一个视图中显示历史。
在我的模型中,每个文件都有一个版本,一个名字和一个路径。该文件与一个对多关系的另一个类A相关。
我想要一个替换前一个文件的更新函数,我也想访问具有文件及其相关模型实例(A类)的所有特征的历史记录。
In my project, I have to handle uploaded files, and I want to have an history of each file version. And to display the history in one view. In my model, each file has a version, a name and a path. This file is related to an other class 'A' by a one to many relationship. I want to have a sort of update function which replace the former file, and I also want to have access to the history with all characteristics of the file and its related model instances (class A).
我不知道该怎么办。我听说过django反转和django修订。
你建议给我什么?
I don't know how to do. I heard about django reversion and django revisions. What do you advise to me ?
谢谢
推荐答案
您可以通过以下几种方法:
You can do it a couple of ways:
1.您可以拥有手动跟踪其的文档文件模型。
1.You can have a document file model which keeps track of it manually.
class DocumentFile(CachedModel):
content_type = models.ForeignKey(ContentType, null=True, blank=True)
object_id = models.PositiveIntegerField(null=True, blank=True)
content_object = generic.GenericForeignKey(ct_field='content_type', fk_field='object_id')
file = models.FileField(upload_to= wherever )
version = models.PositiveIntegerField(default=1)
class Meta:
db_table = 'document_file'
verbose_name = 'Document File'
unique_together = ('document', 'version')
您可以拥有一个名为new_version的post_save信号,并更新当前版本号在文件
You can have a post_save signal called new_version, and update the current revision number on the document
2.你甚至可以使用亚马逊的s3来存储文档,并通过修订版号访问它,获取修订号的get参数(这是一个更昂贵的方法)
2.You May even use amazon's s3 to store the document, and access it by revision number passing it a get parameter for the revision number(This is a costlier approach)
这篇关于Django:模型实例历史的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!