Open In App
Related Articles

Built-in Field Validations – Django Models

Improve Article
Save Article
Like Article

Built-in Field Validations in Django models are the default validations that come predefined to all Django fields. Every field comes in with built-in validations from Django validators. For example, IntegerField comes with built-in validation that it can only store integer values and that too in a particular range. Similarly, every field has its own validations. For more, visit Django Models.

Demonstration of Built-in Validations to Fields in Django

Consider a project named geeksforgeeks having an app named geeks.  

Refer to the following articles to check how to create a project and an app in Django. 

Enter the following code into file of geeks app.  


from django.db import models
from django.db.models import Model
# Create your models here.
class GeeksModel(Model):
    geeks_field = models.IntegerField()
    def __str__(self):
        return self.geeks_field

After running makemigrations and migrate on Django and rendering above model, let us try to create an instance using string “GfG is Best“. 


You can see in the admin interface, one can not enter a string in an IntegerField. Similarly, every field has its own validations.

Add more Built-in Validations to Field

Django has choices of fields for almost every data you want to store in database such as IntegerField for integer and CharField for strings. But there are some built-in validations which you can apply on these fields too. For example, unique=True will limit the entries of a particular field to unique entries. Below is a list of built-in validations you can use for your field to make more changes.

Field Options Description
Null If True, Django will store empty values as NULL in the database. Default is False.
Blank If True, the field is allowed to be blank. Default is False.
db_column The name of the database column to use for this field. If this isn’t given, Django will use the field’s name. 
Default The default value for the field. This can be a value or a callable object. If callable it will be called every time a new object is created. 
help_text Extra “help” text to be displayed with the form widget. It’s useful for documentation even if your field isn’t used on a form. 
primary_key If True, this field is the primary key for the model.
editable If False, the field will not be displayed in the admin or any other ModelForm. They are also skipped during model validation. Default is True
error_messages The error_messages argument lets you override the default messages that the field will raise. Pass in a dictionary with keys matching the error messages you want to override. 
help_text Extra “help” text to be displayed with the form widget. It’s useful for documentation even if your field isn’t used on a form. 
verbose_name A human-readable name for the field. If the verbose name isn’t given, Django will automatically create it using the field’s attribute name, converting underscores to spaces. 
validators A list of validators to run for this field. See the validators documentation for more information. 
Unique If True, this field must be unique throughout the table. 


Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 17 May, 2021
Like Article
Save Article
Similar Reads
Related Tutorials