title: Autoincrement-like field for objects with the same foreign key (Django 1.8, MySQL 5.5)
body: <p>I've got a Django model (let's call it ObjectLog), many of which are related to a single Object by a foreign key.  See below for a brief definition:</p>
  
  <pre><code>class ObjectLog(models.Model):
      class Meta:
          ordering = ['-created','-N']
          unique_together = ("object","N")
      object = models.ForeignKey(Object, null=False)                                                                                                                                                              
      created = models.DateTimeField(auto_now_add=True)
      issuer = models.ForeignKey(User)
      N = models.IntegerField(null=False)
  </code></pre>
  
  <p>Each ObjectLog which is related to a single Object should have a unique N value (as shown by the <code>unique_together</code> requirement).  Another way of putting this is that N should essentially be an autoincrement field, but only relative to the ObjectLog set for an individual Object. This is not a difficult thing to accomplish logically with a custom save method:</p>
  
  <pre><code>def save(self, *args, **kwargs):                                                                                                                                                                        
      with transaction.atomic():
          logs = self.object.objectlog_set.select_for_update().order_by('-N')
          if logs:
              self.N = logs[0].N + 1
          else:
              self.N = 1
          super(ObjectLog, self).save(*args, **kwargs)
  </code></pre>
  
  <p>However, I'm having trouble dealing with concurrency. When multiple processes are simultaneously trying to create ObjectLogs for a single Object, they often end up with the same value of N, resulting in errors like "Duplicate entry \'249244-169\' for key".  I've tried to deal with this by using <code>transaction.atomic</code> and <code>select_for_update</code> in my <code>save</code> method, although I'm now realizing that atomicity is not really the property that will help me here.  It looks like I need some way of locking the rows in the ObjectLog table related to the Object in question while the code determines what N should be and saves the new row, but I can't figure out how to do this.</p>
  
  <p>I'm using Django 1.8 and MySQL 5.5 with a MyISAM engine.  I've also tried modifying the table in question to use InnoDB.  When using InnoDB, I get errors like "1213, \'Deadlock found when trying to get lock; try restarting transaction\'".  So it seems as though locking is working in this scenario, but maybe I'm overdoing it?  A nasty solution would be to catch these errors and use a while loop to force the save method to retry, but I'd really rather not.</p>
  
  <p>Any suggestions are much appreciated!  Apologies if I've misused some terminology, I'm fairly new to the scene.  I'm happy to provide more information if I've missed something of importance.</p>