Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django difference between validator and clean_field method

In a form in django, what is the difference between a validator for a field and a clean_<field> method for that field?

like image 304
Navid777 Avatar asked Mar 29 '14 20:03

Navid777


1 Answers

Django will first run the built-in (default) field validators, then your custom field validators (using validators=[your_validator] in your models). Then, Django will then execute the clean() and clean<field>() methods.

The main difference between a validator and a clean_<field>() method is that the latter is only meant for forms. A validator can be used for both your forms and your models (and hence will also be used in e.g. the admin interface).

Also, overriding the clean_<field>() method is the recommended way to validate data against items in your database.

More info on https://docs.djangoproject.com/en/1.6/ref/forms/validation/.

like image 92
SaeX Avatar answered Sep 19 '22 16:09

SaeX