Unique BooleanField value in Django? Unique BooleanField value in Django? database database

Unique BooleanField value in Django?


Whenever I've needed to accomplish this task, what I've done is override the save method for the model and have it check if any other model has the flag already set (and turn it off).

class Character(models.Model):    name = models.CharField(max_length=255)    is_the_chosen_one = models.BooleanField()    def save(self, *args, **kwargs):        if self.is_the_chosen_one:            try:                temp = Character.objects.get(is_the_chosen_one=True)                if self != temp:                    temp.is_the_chosen_one = False                    temp.save()            except Character.DoesNotExist:                pass        super(Character, self).save(*args, **kwargs)


I'd override the save method of the model and if you've set the boolean to True, make sure all others are set to False.

from django.db import transactionclass Character(models.Model):    name = models.CharField(max_length=255)    is_the_chosen_one = models.BooleanField()    def save(self, *args, **kwargs):        if not self.is_the_chosen_one:            return super(Character, self).save(*args, **kwargs)        with transaction.atomic():            Character.objects.filter(                is_the_chosen_one=True).update(is_the_chosen_one=False)            return super(Character, self).save(*args, **kwargs)

I tried editing the similar answer by Adam, but it was rejected for changing too much of the original answer. This way is more succinct and efficient as the checking of other entries is done in a single query.


Instead of using custom model cleaning/saving, I created a custom field overriding the pre_save method on django.db.models.BooleanField. Instead of raising an error if another field was True, I made all other fields False if it was True. Also instead of raising an error if the field was False and no other field was True, I saved it the field as True

fields.py

from django.db.models import BooleanFieldclass UniqueBooleanField(BooleanField):    def pre_save(self, model_instance, add):        objects = model_instance.__class__.objects        # If True then set all others as False        if getattr(model_instance, self.attname):            objects.update(**{self.attname: False})        # If no true object exists that isnt saved model, save as True        elif not objects.exclude(id=model_instance.id)\                        .filter(**{self.attname: True}):            return True        return getattr(model_instance, self.attname)# To use with Southfrom south.modelsinspector import add_introspection_rulesadd_introspection_rules([], ["^project\.apps\.fields\.UniqueBooleanField"])

models.py

from django.db import modelsfrom project.apps.fields import UniqueBooleanFieldclass UniqueBooleanModel(models.Model):    unique_boolean = UniqueBooleanField()    def __unicode__(self):        return str(self.unique_boolean)