Open In App

unique=True – Django Built-in Field Validation

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

Built-in Field Validations in Django models are the validations that come predefined to all Django fields. Every field comes in with built-in validations from Django validators. One can also add more built-in field validations for applying or removing certain constraints on a particular field. unique=True sets the field to be unique i.e. once entered a value in a field, the same value can not be entered in any other instance of that model in any manner. It is generally used for fields like Roll Number, Employee Id, etc which should be unique. 

Syntax:

field_name = models.Field(unique=True)

Django Built-in Field Validation unique=True Explanation

Illustration of unique using an Example. 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. We will be using CharField for experimenting for all field options. 

Python3




from django.db import models
from django.db.models import Model
# Create your models here.
 
class GeeksModel(Model):
    geeks_field = models.CharField(
                    max_length = 200
                    unique = True
                    )


After running makemigrations and migrate on Django and rendering the above model, let us create an instance from Django admin interface with string “a“. Now to show the constraint of unique=True, let us try to create one more instance of the model using same string. Now it will show this error. django-error-messages

Advanced concepts with unique

This is enforced at the database level and by model validation. If you try to save a model with a duplicate value in a unique field, a django.db.IntegrityError will be raised by the model’s save() method. This option is valid on all field types except ManyToManyField and OneToOneField. Note that when unique is True, you don’t need to specify db_index, because unique implies the creation of an index.

More Built-in Field Validations

.math-table { border-collapse: collapse; width: 100%; } .math-table td { border: 1px solid #5fb962; text-align: left !important; padding: 8px; } .math-table th { border: 1px solid #5fb962; padding: 8px; } .math-table tr>th{ background-color: #c6ebd9; vertical-align: middle; } .math-table tr:nth-child(odd) { background-color: #ffffff; } 

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.


Last Updated : 26 Oct, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads