Suppose my models.py is like so:
class Character(models.Model):
name = models.CharField(max_length=255)
is_the_chosen_one = models.BooleanField()
I want only one of my Character
instances to have is_the_chosen_one == True
and all others to have is_the_chosen_one == False
. How can I best ensure this uniqueness constraint is respected?
Top marks to answers that take into account the importance of respecting the constraint at the database, model and (admin) form levels!
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…