Note : primary_key = True
means null = False
and unique = True
. Only one primary key is allowed per object.
Syntax
field_name = models.Field (primary_key = True)
Django built-in field validation primary_key = True
Explanation
Illustration of primary_key = True using example. Consider a project named pythonengineering
that has an application named geeks
.
Refer to the following articles to check how to create a project and an app in Django.
Enter the following code into the models.py
file of the geeks application. We’ll use IntegerField to experiment with the primary_key.
|
After starting As for the makemigrations and migrations to Django and the rendering of the above model, let’s try creating an instance using None from the Django shell. To start the Django shell, enter the command,
Python manage.py shell
Now let’s try to create a GeeksModel instance using None .
|
Let’s check in the admin interface if the model is instantiated. Latest questions
Therefore, primary_key = True changes the field to the PRIMARY KEY for this table. To learn more about the Primary Key,
Shop