How to Create a Custom Field Validator in Django


In this article, we show how to create a custom field validator in Django.

So, let's say we want to validate a field in a custom way, such as check an email field to see if it contains .edu (to see if it is a school email) or check an email to see if it has the name nyit (to see if the email is a valid nyit email).

In Django, it is simple to create custom field validators, so that we can validate a field to see if it meets the parameter(s) we require. If it doesn't, we can raise a ValidationError and output a message to the user.

So, in this example, we will demonstrate building a custom field validator in Django that validates an email field of a database field to see whether it contains a valid school email (contains .edu).

So, let's first go to the file. File

In this file, we create a simple database table called Student.

We have 2 fields, just for simplicity, name and emailaddress.

So, to create a database table, we need to import models from django.db

Later, we will create a file and from this file import a custom validating function that we create called, validate_school_email

We then create our database table called Student and give it 2 fiedls: name and emailaddress.

The emailaddress field is of type EmailField. This means that it has built-in email validation from Django. However, this is not enough. We want to take this a step further and validate the email address to see whether it is a school email address. School email addresses normally have '.edu'. Therefore, we're going to create a function that checks for this. The function name will be validate_school_email.

We create an attribute in the emailaddress field called validators and set this equal to a list of functions, in which we add one function, validate_school_email.

Now that we have this function to validate the email, we have to create it. File

So what you want to do is create a separate file, you can call it

And in this file, we want to put your validating functions.

The reason why you would want to do it this way is because you can use these validating functions in many places on your website.

You may use it for a database table, you then may it for a form, etc.

So you would want to have it separately so that you can just call it from its original file (

So below is the code to check whether the email address has ".edu" in it

So in order to use the function, ValidationError, you have to import this from django.core.exceptions

This ValidationError function is a function that causes a validation error if the data that a user enters doesn't pass validation.

So once we've imported this, we create the function validate_school_email and pass in the parameter value.

We create an if statement that if ".edu" is not in value, we cause a validation error that states, "A valid school email must be entered in"

Else, we just return the (validated) data.

Below is an image of the database field with the validation in place.

Custom validation in Django

So you can see that a non-school email such as gmail does not pass validation as a school email.

Notice that in the file in the emailaddress field, the validators attribute accepts a list. So you can specify however many validators that you want or need for a given validation.

For example, you could add another function to provide any validation process.

For example, besides checking to see if it contains ".edu", I also want to make sure that it contains "nyit" (that it is a valid nyit email address).

So I can add another validating function to check whether the email address contains "nyit"

So I go back to the file and add another function.

The entire file will now contain the following code, shown below.

So now this new function, validate_nyit_email checks to see whether the email address contains "nyit" in it.

If it does not, a validation error is raised that states, "The email must be from nyit"

If it does contain "nyit", the result is simply returned.

So after we create this function, we then go back into the file and add this function to the validators attribute.

So the full line of code for the emailaddress database table field will be, as follows.

So the emailaddress field will now be validated by 2 functions.

Remember that you must also import this new function that you created in the file.

So now if we go back to admin and put in the email,, we get 2 validation errors.

This is shown below.

Custom validations in Django

So you can see we now get 2 validation errors.

And this how we can create custom field validators in Django.

So this is all that is needed to create a custom field validator in Django.

Related Resources

How to Randomly Select From or Shuffle a List in Python

HTML Comment Box is loading comments...