[Answered ]-Limit the number of records in a Model that can have a value per user

2👍

You should create custom clean method on your model.

from django.core.exceptions import ValidationError
from django.db import models

class MyModel(models.Model):
    user = models.ForeignKey(User)
    is_active = models.BooleanField(default=False)
    #...more fields ...

    def clean(self):
        if not self.pk and MyModel.objects.filter(user=self.user, is_active=True).exists():
            raise ValidationError('How about no?')

Leave a comment