how to use uuid instead of id as the primary key primary in django

created at 07-16-2021 views: 1

Whenever we create any new model, an ID field is appended. By default, the data type of the ID field will be Integer.

In order to make the id field a UUID, a new field type UUIDField was added in Django 1.8+.

from django.db import models

class Event(models.Model):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    details = models.TextField()
    years_ago = models.PositiveIntegerField()

>>> eobj = Event.objects.all()
>>> eobj.first().id
'1cd1b4b0c35f43488a92b3bb42029f46'
Please log in to leave a comment.