title: Limit a Django ManyToMany by field value
body: <p>Given the following models, how can I limit the number of ManyToMany relationships that can be created to the value of "count" in the Key model?</p>
  
  <p>For example, if "count" is 2 then only 2 devices can use that key.</p>
  
  <p>Models:</p>
  
  <pre><code>class Device(models.Model):
      name = models.CharField(max_length=100, unique=True)
  
  class Key(models.Model):
      key = models.CharField(max_length=100, unique=True)
      count = models.IntegerField(default=1)
      device = models.ManyToManyField(Device, blank=True, null=True)
  </code></pre>
  
  <p>I would need to raise an exception if the user tries to add a key to too many devices, or if they reduce "count" to less than the number of devices already using the key. A device can have any number of keys.</p>
  
  <p>(I'm using the admin site for data entry)</p>