How to add validation to your Django project?

Validation is the process through which the computer automatically checks to ensure that the data entered is sensible and reasonable. It does not provide if the data entered is accurate or not. Many of us are familiar with email or phone validation that is usually a part of most websites. When we enter the email address in the wrong format or if the phone number entered does not contain 10 digits, a warning is usually displayed to enter the output in the accepted format. This is validation. Most developers add validation to their projects to ensure that the data they receive from the users in the format they desire.

Validation in Django

Validation in your Django project can be done on forms and fields. Form validation happens when the data is cleaned. If your want to customize this process, there are multiple areas that need to be changed. Usually, each of these places serve a different purpose.

Validation takes place when the data is cleaned. There are three ways in which cleaning occurs in django during form processing. Generally, these are executed upon a call to the is_valid() method on a form. There are other things that can also trigger cleaning and validation such as calling the full_clean() method.

Validation is performed by validators; these are helpers that can be reused. Validators are functions that take a single argument and raise a ValidationError on invalid input. After a call is made to a fields to_python and validate methods, the validators are run.

The process of validating a form in django is split into several steps. These steps are as follows.

  • The to_python method on a field is the first step in every validation. It makes sure that the data entered belongs to the correct datatype and raises a ValidationError otherwise. This method accepts data entered by the user, translates it into the python equivalent data type or raise an error if that is not possible.

    Eg: FloatField will raise a ValidationError if the data entered in this field cannot be turned into a Python float.

  • The validate() method handles specific validations on a field that are not suitable for validators. It handles field-specific errors only. It takes a value that has been given by the user and converts it to a correct datatype and raises ValidationError on any abnormal behavior.

  • The run_validators() method runs all the validators applied to a particular field and combines all the errors generated into a single Validation Error.

  • Like mentioned before, validators are run when data is cleaned. So the clean() methods ensures that the to_python(),validate() and run_validators() are run when needed, in the correct order and also ensures that an error is raised if needed.

  • For example, to validate the contents of a CharField called idnumber which uniquely identifies a row, clean_idnumber() would be method in which the cleaning process would be specified. The return value of this method replaces the existing value of the cleaned data.