Open In App

Field Validations and Built-In Fields - Django Models

Last Updated : 21 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Field validations in Django help make sure the data you enter into your database is correct and follows certain rules. Django automatically checks the data based on the type of field you use, so you don’t have to write extra code to validate it yourself.

Django provides built-in validations for every field type, making it easier for developers to enforce rules without writing extra code.

How Do Built-in Validations Work

Each type of field in Django has its own built-in checks. For example:

  • IntegerField only accepts whole numbers.
  • CharField is for text and limits how long the text can be.
  • DateField checks that the value is a valid date.

If you try to save wrong data, Django will give an error and won’t let the data be saved.

Demonstration of Built-in Validation

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 models.py file of geeks app.  

Python
from django.db import models

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

    def __str__(self):
        return str(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". 

built-in-validation-django-models


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

Extra Built-in Validations You Can Use

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 OptionsDescription
NullIf True, Django will store empty values as NULL in the database. Default is False.
BlankIf True, the field is allowed to be blank. Default is False.
db_columnThe name of the database column to use for this field. If this isn’t given, Django will use the field’s name. 
 
DefaultThe 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_textExtra “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_keyIf True, this field is the primary key for the model.
editableIf 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_messagesThe 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_textExtra “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_nameA 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. 
 
validatorsA list of validators to run for this field. See the validators documentation for more information. 
 
UniqueIf True, this field must be unique throughout the table. 
 

Read Next: Custom Field Validations in Django Models


Next Article
Practice Tags :

Similar Reads