I have a Model with a boolean field named is_active. I want to limit the number of models a user can have where the value of that boolean field is True to 1.
class MyModel(models.Model):
user = models.ForeignKey(User)
is_active = models.BooleanField(default=False)
#...more fields ...
Initially I was just going to add a unique_togeather = ("user", "is_active") entry to Meta -- but of course that will also limit the number of False entries a User can have -- which I need to be unlimited.
Ideally, I'd like to solve this at the database level to prevent race conditions.
The models are being created by Celery tasks importing data using MyModel.objects.get_or_create() -- so it is very possible that a race condition could occur due to the level of concurrency from Celery workers.