Question

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

Was it helpful?

Solution

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/.

OTHER TIPS

As far as I remember a field can have several validators (like min_length, max_length) which will be called by the default clean_field method.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top