Unique BooleanField value in Django?

前端 未结 13 851
清酒与你
清酒与你 2020-12-07 14:11

Suppose my models.py is like so:

class Character(models.Model):
    name = models.CharField(max_length=255)
    is_the_chosen_one = models.BooleanField()
         


        
13条回答
  •  情深已故
    2020-12-07 14:45

    Using a similar approach as Saul, but slightly different purpose:

    class TrueUniqueBooleanField(BooleanField):
    
        def __init__(self, unique_for=None, *args, **kwargs):
            self.unique_for = unique_for
            super(BooleanField, self).__init__(*args, **kwargs)
    
        def pre_save(self, model_instance, add):
            value = super(TrueUniqueBooleanField, self).pre_save(model_instance, add)
    
            objects = model_instance.__class__.objects
    
            if self.unique_for:
                objects = objects.filter(**{self.unique_for: getattr(model_instance, self.unique_for)})
    
            if value and objects.exclude(id=model_instance.id).filter(**{self.attname: True}):
                msg = 'Only one instance of {} can have its field {} set to True'.format(model_instance.__class__, self.attname)
                if self.unique_for:
                    msg += ' for each different {}'.format(self.unique_for)
                raise ValidationError(msg)
    
            return value
    

    This implementation will raise a ValidationError when attempting to save another record with a value of True.

    Also, I have added the unique_for argument which can be set to any other field in the model, to check true-uniqueness only for records with the same value, such as:

    class Phone(models.Model):
        user = models.ForeignKey(User)
        main = TrueUniqueBooleanField(unique_for='user', default=False)
    

提交回复
热议问题