Table of contents
1.
Introduction
2.
What is Built-in Field Validations in Django models?
3.
Add more options
4.
Coding Example
5.
FAQs
6.
Key takeaways
Last Updated: Mar 27, 2024

Built-in Field Validations in Django models

Author Ranjul Arumadi
2 upvotes
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Django is a high-level python web framework. It is used to create and maintain secure websites. Django is used to create tables and fields called the Django model. Almost all the websites that we use will have fields. We can find fields in forms. Django gives built-in form validations. Built-in field validations in Django models are predefined in Django. Let us see more about built-in field validation in Django models.

What is Built-in Field Validations in Django models?

Built-in field validations in Django models are predefined in Django. They are default validations. Every field in Django has validations that are unique to it. The module django.core.validators contain variables that can be called. We can use the callable variables for the model and field. These callable variables are for internal use but can also be used for our field.

 

Some examples for Built-in Field Validations in Django models are RegexValidator,EmailValidator,URLValidator,Validate_email,Validate_slug,Validate_unicode_slug,Validate_ipv4_address etc.

 

Built-in Field Validations

Description 

Syntax 

RegexValidator
  • If the regex is not assigned as None, it allows regex overrides. This can be a pre-compiled regular expression. 
  • If the message is not set as none, it will override the message. If the code is not assigned as none, it will enable overriding code. 
  • If the inverse_match is not assigned as none, then the inverse_match is overridden. 
  • If the flag is not set to none, then overriding of flags is allowed. In such cases, the regex must be a regular expression. 
  • If regex is not a regular expression string, a TypeError is raised.

The RegexValidator searches the provided value with re.search(). The RegexValidator, by default, gives a ValidationError with message ad code. This is done when the match is not found. If the inverse_match is set to True, we can obtain an opposite result.

Class RegexValidator(regex=None,message=None,code=None,inverse_match=None,flag=0)
You can also try this code with Online Python Compiler
Run Code

 

 

EmailValidator

 

When not set to None, the message allows overriding of the message. Similarly, when code and allowlist are not set to None, it will enable overriding code and allowlist.
class EmailValidator(message=None,code=None,allowlist=None)
You can also try this code with Online Python Compiler
Run Code

 

 

URLValidator

A value must look like a URL. This is checked with the RegexValidator subclass. It gives an error code “invalid”.
class URLValidator(scheme=None,regex=None,message=None,code=None)
You can also try this code with Online Python Compiler
Run Code

Validate_email

 

It is an EmailValidator instance, and it has no customizations.
validate_email
You can also try this code with Online Python Compiler
Run Code
Validate_slug It takes care of a value that has only letters, numbers, hyphens and underscores. It is a RegexValidator instance.
validate_slug
You can also try this code with Online Python Compiler
Run Code

Validate_unicode_slug

 

It takes care of values with Unicode letters, numbers, hyphens and underscores. It is a RegexValidator instance.
validate_unicode_slug
You can also try this code with Online Python Compiler
Run Code
Validate_ipv4_address It is used to check the validity of the IPv4 address. 
validate_ipv4_address
You can also try this code with Online Python Compiler
Run Code
Validate_ipv6_address It is used to check the validity of the IPv6 address. They are used in django.utils.ipv6.
validate_ipv6_address
You can also try this code with Online Python Compiler
Run Code

 

int_list_validator Ensured that sep separates the values By setting allow_negative as True, negative integers can be set.
int_list_validator(sep=', ', message=None, code='invalid', allow_negative=False)
You can also try this code with Online Python Compiler
Run Code
MaxValueValidator A ValidationError is set with “max_value”  when the limit_value is greater than the value.
class MaxValueValidator(limit_value, message=None)
You can also try this code with Online Python Compiler
Run Code
MinValueValidator A ValidationError is set with “min_value”  when the limit_value is lesser than the value.
class MinValueValidator(limit_value, message=None)
You can also try this code with Online Python Compiler
Run Code
MaxLengthValidator A ValidationError is set with “max_length”  when the limit_value is greater than the value.
class MaxLengthValidator(limit_value, message=None)
You can also try this code with Online Python Compiler
Run Code
MinLengthValidator A ValidationError is set with “min_length”  when the limit_value is lesser than the value.
class MinLengthValidator(limit_value, message=None)
You can also try this code with Online Python Compiler
Run Code

Add more options

Some of the built-in field validations in Django models that be used to make changes are:-

Fields Options

Description

Null Default is False. Django will store the values as Null in the database if set True.
Blank Default is False. If True, the field is allowed to be blank.
Db_column Django will use the field's name if no values are given.
Default Every time a new object is created, this will be called.
Help text Used to display text with the form widget.
Primary key If set to True, then the field is the model's primary key.
Editable If set to false, Django will not display the field.
Error message Lets us override the default message of the field.
Verbose name Name for the field that humans can read.
Unique If set to true, then the field must be unique.

Coding Example

Let us see a code example to understand validations. Consider a project named ninjas having an app called validation. Enter the following code into the models.py file of the validation app. 

 

Code:

from django.db import models
from django.db.models import Model
# Create your models here.

class GeeksModel(Model):
input_field= models.IntegerField()

def __str__(self):
return self.input_field
You can also try this code with Online Python Compiler
Run Code

 

Run the following terminal commands:

python3 manage.py makemigrations validation
python3 manage.py migrate 

 

After rendering the above model, create an instance using the "Django is cool "string. If we check the admin interface, we can see that we cannot enter a string in an IntegerField.

 

Output:

FAQs

  1. What is a model in Django?
    Django Model is an inbuilt feature in Django that is used to create tables in the database, and generally, a model in Django is a class used to store essential methods and fields.
     
  2. What is the use of the Validate_ipv4_address build-in validation?
    The Validate_ipv4_address is used to check the validity of the IPv4 address.

Key takeaways

Django is used to create and maintain secure websites. There is a built-in feature(SQL of database) in Django used to create their tables and fields called the Django model. Every field has built-in field validations in Django models. Built-in field validations in Django models are predefined in Django.

 

If you loved reading this article about built-in field validations in Django models, check out Flask vs Django in 2021: Which Framework to Choose? and Advantages & Disadvantages of Django.

Live masterclass