当前位置: 动力学知识库 > 问答 > 编程问答 >

django - What is the proper place to put ModelForm validation?

问题描述:

I'm using Django's ModelForms and would like to have validation on both models and forms. I'm rendering form using:

{{ form.as_table }}

What is the proper place to put validation on per-field basis (like forms' clean_fieldname) and as a whole (like forms' clean)?

I'd like to minimize amount of code written and not write the same validators twice (in both models and forms). Also, I'd like per-field validation errors to be shown over proper form field, not have all errors placed on top of the form (so not all checks in models' clean).

网友答案:

You might want to use the custom validators introduced in Django 1.2.

Don't forget that unless you are using a ModelForm you have to explicitly performs the model validation with a call to the full_clean() method on your model instance.

分享给朋友:
您可能感兴趣的文章:
随机阅读: