In Django, How do I deal with concurrent changes to the Images associated with a Post object?
This is a flavour of question that has been asked before, but not quite covering the same issues. I've read through these (question, question, question, and question) but the issue is slightly different.
I have a blog post model (pseudocode for speed), which contains title, abstract and body, and associated Images.
class Post(models.Model):
title = CharField
abstract = TextField
body = TextField
class Image(models.Model):
post = ForeignKey(Post)
imagefile = ImageField
Now, what I want to add is the ability to store histories of the changes to this Post
model. I've thought of two possibilities for this:
Possibility 1
class PostHistory(models.Model):
post = ForeignKey(Post)
title_delta = TextField
abstract_delta = TextField
body_delta = TextField
However this has the issue that it is storing deltas for no changes (for example when title
does not change and there is only a delta for the body
field. That said, when more than one field changes, it fits that '1 revision == 1 complete revision'.
Possibility 2
class PostRevision(models.Model):
post = ForeignKey(Post)
field = CharField #Field name
delta = TextField
Through two different approaches, this successfully gives me a history of diffs for the field, which I would generate using diff-match-patch (slightly more performant than the inbuilt difflib). The two issues I now have are related to the generation of master objects (i.e. the top revision in the chain).
The question being asked is: How do I deal with concurrent changes to the Images associated with a Post object? These would be changed via references within the body
field of the Post
model (this is a Markdown formatted text field which is then edited on POST
of the form to add in the URL references for the image field). Is the best way to deal with this to use an M2M field on the revision, and on the Post
object, allowing the images to be always stored with the PostRevision
object?