Search code examples
djangorevision-history

Django : Model Instance history


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


Solution

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