A
A
Anton Manevant2013-12-17 14:47:18
Django
Anton Manevant, 2013-12-17 14:47:18

Why and when should you use clean and clean_fieldname to validate a form field?

Actually a question. I do not quite understand why and when you need to use clean and clean_fieldname to check the form field. What is the difference? When is the best time to use?
In general, form validation in Django is a bit mind blowing.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
alternativshik, 2013-12-17
@Manevant

clean_fieldname is used to validate the data of a SPECIFIC form field and is responsible for calling the to_python, validate and run_validators methods in the correct order and passing their errors. As soon as any of these methods throws a ValidationError exception, the validation process is terminated and the error is passed up. This method returns the validated data, which is then placed into the form's cleaned_data dictionary.
The form child's clean() method can perform any validation that needs to access data from multiple fields at the same time. This is where you can check that if field A is filled, then field B must contain a valid email address, and so on. The data returned by this method is placed in the cleaned_data property of the form.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question