Custom Field Validations in Django Models
Last Updated :
10 Oct, 2025
Custom field validations ensure that data entered into a model field meets specific rules before being saved to the database. They extend Django’s built-in validations, allowing developers to enforce additional rules.
- Can verify formats, length limits, or complex conditions (e.g., ensuring an email belongs to a certain domain).
- Run automatically when creating or updating model instances.
- Help maintain clean and consistent data without adding extra validation logic in forms or views.
- Builds on Django’s built-in field validations.
Syntax
field_name = models.Field(validators=[validator_function1, validator_function2])
- validators is a list of validator functions that will be executed when the field value is set.
- Each validator function accepts a single argument, value, which is the data entered for the field.
Example: A custom validator to only accept email addresses ending with @gmail.com. Consider a project named 'geeksforgeeks' having an app named 'geeks'.
1. Define the Model
In models.py, start with a simple model having a CharField for email:
Python
from django.db import models
from django.db.models import Model
class GeeksModel(Model):
geeks_mail = models.CharField(max_length = 200)
2. Create a Validator Function
Create a custom validator function that will check whether the email address ends with @gmail.com. If it doesn't, the function will raise a ValidationError.
Python
from django.core.exceptions import ValidationError
def validate_geeks_mail(value):
if not value.endswith("@gmail.com"):
raise ValidationError("This field accepts only Gmail addresses.")
3. Attach the Validator to the Model Field
Attach this validator function to the geeks_mail field in our model using the validators parameter:
Python
from django.db import models
from django.core.exceptions import ValidationError
def validate_geeks_mail(value):
if not value.endswith("@gmail.com"):
raise ValidationError("This field accepts only Gmail addresses.")
class GeeksModel(models.Model):
geeks_mail = models.CharField(max_length=200, validators=[validate_geeks_mail])
Now, the geeks_mail field will only accept email addresses containing @gmail.com. Any other email address will trigger a ValidationError. Let us try to create an instance without gmail.com and check if our validation worked or not.
After every change in models.py, run these commands makemigrations and migrate commands to update the changes in database.
Visit: http://localhost:8000/admin/geeks/geeksmodel/add/ and enter "[email protected]":
Custom ValidationThe custom validator ensures that the geeks_mail field only accepts email addresses ending with @gmail.com. Any other email address triggers a ValidationError. This approach allows applying any kind of custom validation to a field’s value.
Explore
Python Fundamentals
Python Data Structures
Advanced Python
Data Science with Python
Web Development with Python
Python Practice