Unique BooleanField value in Django?

前端 未结 13 791
清酒与你
清酒与你 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:57
    class Character(models.Model):
        name = models.CharField(max_length=255)
        is_the_chosen_one = models.BooleanField()
    
        def clean(self):
            from django.core.exceptions import ValidationError
            c = Character.objects.filter(is_the_chosen_one__exact=True)  
            if c and self.is_the_chosen:
                raise ValidationError("The chosen one is already here! Too late")
    

    Doing this made the validation available in the basic admin form

    0 讨论(0)
提交回复
热议问题