I have Django webapp with different content (e.g. Posts, Threads, comments) and I want to keep track of their history. So I'm trying to create a "version-control" for "content". This is how I'm doing it (skip to the end for the question):
I have a model Create_Content which represents an action: has an actor (editor), a set of modifications (edition), a timestamp, and the content it acts on:
class Create_Content(models.Model):
editor = models.ForeignKey('User')
edition = models.TextField() # pickled dictionary
date = models.DateTimeField(auto_now_add=True)
content = models.ForeignKey('Content')
which is saved exactly once because history cannot be changed.
I define content has:
class Content(models.Model):
last_edit = models.ForeignKey(Create_Content, related_name='content_last_id')
first_edit = models.ForeignKey(Create_Content, related_name='content_first_id')
def author(self):
return self.first_edit.editor
where last_edit is the last action performed it, and first_edition I use to get the initial author of the content.
All the actual content of the webapp is implemented by derived models of Content, which are created/edited by derived models of Create_Content. For instance,
class Comment(Content):
post = models.ForeignKey(Post, related_name="comment_set")
body = models.TextField()
is created during the save of Create_Comment(Create_Content) (in fact all subclasses of Create_Content are proxies).
For instance, user editions are instances of a subclass of Create_Content (e.g. Edit_Comment(Create_Content)) which, during save(), makes last_edit to point onto self and updates the actual content (e.g. Edit_Comment.body<