Open In App
Related Articles

primary_key – Django Built-in Field Validation

Improve Article
Improve
Save Article
Save
Like Article
Like

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. One can also add more built-in field validations for applying or removing certain constraints on a particular field. primary_key=True will make the field PRIMARY KEY for that table (model).If you don’t specify primary_key=True for any field in your model, Django will automatically add an AutoField to hold the primary key, so you don’t need to set primary_key=True on any of your fields unless you want to override the default primary-key behavior. For more, see Automatic primary key fields.

Note: primary_key=True implies null=False and unique=True. Only one primary key is allowed on an object.

Syntax

field_name = models.Field(primary_key = True)

Django Built-in Field Validation primary_key=True Explanation

Illustration of primary_key=True 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 IntegerField for experimenting for primary_key.




from django.db import models
from django.db.models import Model
# Create your models here.
  
class GeeksModel(Model):
    geeks_field = models.IntegerField(primary_key = True)

After running makemigrations and migrate on Django and rendering the above model, let us try to create an instance using None from Django shell. To start Django shell, enter the command,

Python manage.py shell

Now let us try to create instance of GeeksModel using None.




# importing required model
from geeks.models import GeeksModel
  
# creating instance of GeeksModel
s = GeeksModel.objects.create(geeks_field = 12)
  
# saving current model instance
s.save()

Let us check in admin interface if the instance of model is created.
primary_key=True - Django Built-in Field Validation
Therefore, primary_key=True modifies the field to as PRIMARY KEY for that table. To know more about Primary key, visit here.

Advanced Concepts with primary_key=True

The primary key field is read-only. If you change the value of the primary key on an existing object and then save it, a new object will be created alongside the old one.

More Built-in Field Validations

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.
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.

Last Updated : 19 Feb, 2021
Like Article
Save Article
Similar Reads
Related Tutorials