So basically my app has users of course and each user can create 5 instances of “ModelA”. Simple enough, but there is also “ModelB” with a relation to “ModelA” and the user model. I want the user to be able to create a total of 15 “ModelB” instances, but each “ModelA” instance can only have 5 “ModelB” instances tied to it?
Any tips?
The way I handle the first part for 5 “ModelA” instances per user is like this:
def clean(self):
new_instance = self.__class__
if (new_instance.objects.count() > 4):
raise ValidationError(
"Users may only create 5 %s." % new_instance.verbose_name_plural
)
super(ModelA, self).clean()
Thanks
EDIT:(Built in Django Users functionality assumed)
class ModelB(models.Model):
user = models.ForeignKey(User)
modelA = models.ForeignKey('ModelA')
other_field = models.CharField(max_length=50)
class ModelA(models.Model):
user = models.ForeignKey(User)
other_field = models.CharField(max_length=50)
Basically The user can create 5 ‘ModelA’ instances and for each of those instances they can create 3 ‘ModelB’ instances.
How can I do this within the model logic?
thanks
Does this work?