开发者

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜