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?
For example, if "count" is 2 then only 2 devices can use that key.
Models:
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)
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.
(I'm using the admin site for data entry)