Django : How to ensure a unique value for a field or group of fields in a record / form
I'm using a ModelForm and i'm happy with the default validation and errors.
In my template i use a simple:
{{ form.as_ul }}
with a {% if form.errors %}
block.
The Model contains among other things those fields :
group1_wish1 = models.CharField(max_length=100, choices=GROUP1CHOICES)
group1_wish2 = models.CharField(max_length=100, choices=GROUP1CHOICES)
group2_wish1 ....
group2_wish2 ....
Each user is not allowed to make the same wish twice for each grou开发者_如何学Gop : group_wish1 != group_wish2.
Is there a validation option for models that would let me achieve this ?
I've looked at unique
but it works at the table level.
Any idea how i could it as simply as possible ?
The simplest way should be overriding the form's clean()-method in the definition of your ModelForm (forms.py).
See the Django documentation:
Cleaning and validating fields that depend on each other
ModelForms: Overriding the clean()-method
精彩评论