In Django you have to specify nothing in the model The primary key name is id, the type is integer, and it is generated as a serial number from 1.
Here, as the primary key of the model An example of using UUID (Universally Unique Identifier) is shown.
model.py
from django.db import models
import uuid
class Sample(models.Model):
sampleId = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
The point is the ** primary_key = True ** part below, It is clearly stated here that sampleId is the primary key.
sampleId = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
I want the model object to be assigned automatically when it is created, so Specify ** default = uuid.uuid4 **.
In addition, if you change the primary key without much thought Specify ** editable = False ** because DB integrity is likely to be broken (There is a high possibility that it will be difficult to move here ...)
Recommended Posts