Model History in Django
I agree with @rickard-zachrisson that you should stick to approach #1. I'd make a few subtle changes though (pseudo code btw):
class AbstractPost(models.Model):
title = CharField
abstract = TextField
body = TextField
class Meta:
abstract = True
class Post(AbstractPost):
def save(self):
post = super(Post, self).save()
PostHistory.objects.create(
post=post,
title=post.title,
abstract=post.abstract,
body=post.body,
)
class PostHistory(AbstractPost):
post = ForeignKey(Post)
class Meta:
ordering = ['-pk']
class Image(models.Model):
post = ForeignKey(Post)
imagefile = ImageField
Your latest version will always be in Post
and your change history is in pk
order in PostHistory
which is easy to diff against for changes. I'd duplicate the data because storage is cheap and storing deltas is a pita. If you have multiple edits or want to compare the current version to the original version then deltas are basically useless. Any model changes in AbstractPost are reflected in both Post
and PostHistory
.
Image
is keyed to Post so things stay tidy. You can optionally clean up images in your Post.save() function but I'd probably opt for a post_save signal to keep the code cleaner.