Django : Model Instance history

别说谁变了你拦得住时间么 提交于 2019-12-11 02:05:10

问题


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).

I don't know how to do. I heard about django reversion and django revisions. What do you advise to me ?

Thank you


回答1:


You can do it a couple of ways:

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')

You can have a post_save signal called new_version, and update the current revision number on the document

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)



来源:https://stackoverflow.com/questions/12144863/django-model-instance-history

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!