Unique BooleanField value in Django?

前端 未结 13 864
清酒与你
清酒与你 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:33

    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 BooleanField
    
    
    class 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 South
    from south.modelsinspector import add_introspection_rules
    add_introspection_rules([], ["^project\.apps\.fields\.UniqueBooleanField"])
    

    models.py

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

提交回复
热议问题