title: Model History in Django
body: <p><strong>In Django, How do I deal with concurrent changes to the Images associated with a Post object?</strong></p>
  
  <p>This is a flavour of question that has been asked before, but not quite covering the same issues. I've read through these (<a href="https://stackoverflow.com/q/12144863/781909">question</a>, <a href="https://stackoverflow.com/q/10540111/781909">question</a>, <a href="https://stackoverflow.com/q/4157489/781909">question</a>, and <a href="https://stackoverflow.com/q/1752585/781909">question</a>) but the issue is slightly different.</p>
  
  <p>I have a blog post model (pseudocode for speed), which contains title, abstract and body, and associated Images.</p>
  
  <pre><code>class Post(models.Model):
      title = CharField
      abstract = TextField
      body = TextField
  
  class Image(models.Model):
      post = ForeignKey(Post)
      imagefile = ImageField
  </code></pre>
  
  <p>Now, what I want to add is the ability to store histories of the changes to this <code>Post</code> model. I've thought of two possibilities for this:</p>
  
  <p><strong>Possibility 1</strong></p>
  
  <pre><code>class PostHistory(models.Model):
      post = ForeignKey(Post)
      title_delta = TextField
      abstract_delta = TextField
      body_delta = TextField
  </code></pre>
  
  <p>However this has the issue that it is storing deltas for no changes (for example when <code>title</code> does not change and there is only a delta for the <code>body</code> field. That said, when more than one field changes, it fits that '1 revision == 1 complete revision'.</p>
  
  <p><strong>Possibility 2</strong></p>
  
  <pre><code>class PostRevision(models.Model):
      post = ForeignKey(Post)
      field = CharField #Field name
      delta = TextField
  </code></pre>
  
  <p>Through two different approaches, this successfully gives me a history of diffs for the field, which I would generate using <a href="http://code.google.com/p/google-diff-match-patch/" rel="nofollow noreferrer">diff-match-patch</a> (slightly more performant than the inbuilt <a href="http://docs.python.org/2/library/difflib.html" rel="nofollow noreferrer">difflib</a>). The two issues I now have are related to the generation of master objects (i.e. the top revision in the chain).</p>
  
  <p>The question being asked is: <strong>How do I deal with concurrent changes to the Images associated with a Post object?</strong> These would be changed via references within the <code>body</code> field of the <code>Post</code> model (this is a Markdown formatted text field which is then edited on <code>POST</code> 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 <code>Post</code> object, allowing the images to be always stored with the <code>PostRevision</code> object?</p>