title: Django Model Auto Increment Primary Key Based on Foreign Key
body: <p>I'm trying to figure out how to lay out two of my Django models so that when a new model is saved to the database, its primary key is incremented such that it is the next highest value for all records with the same foreign key.</p>
  
  <p>It's much like <a href="https://stackoverflow.com/questions/3605085/mysql-auto-increment-based-on-foreign-key">this question asked</a>, but I'm wondering how you would do it in Django. Here's an excerpt from the question which demonstrates a similar situation:</p>
  
  <pre><code>id | job_id | title
  0     1        hi
  1     1        hello
  2     1        goodbye
  0     2        hi
  1     2        hello
  </code></pre>
  
  <p>I know <a href="https://docs.djangoproject.com/en/dev/faq/models/#do-django-models-support-multiple-column-primary-keys" rel="noreferrer">you can't have multiple primary keys in a Django model</a> and you can use <a href="https://docs.djangoproject.com/en/dev/ref/models/options/#unique-together" rel="noreferrer">unique_together</a>, but the documentation says it uses the equivalent <code>UNIQUE</code> statement in the <code>CREATE</code> statements. Would </p>
  
  <pre><code>class ModelA(models.Model):
     key = models.PositiveIntegerField(primary_key = True)
     fk = models.ForeignKey(ModelB)
  
     def Meta(self):
        unique_together = ("key", "fk")
  </code></pre>
  
  <p>in the models work with <a href="https://stackoverflow.com/a/3605386/451093">this answer</a> to accomplish what I'm looking for? The relationship between the models is one <code>ModelA</code> having many <code>ModelB</code>s, but each <code>ModelB</code> having only one <code>ModelA</code>.</p>