11. How to use a UUID instead of ID as prmary key?

Whenever we create any new model, there is an ID field attached to it. The ID field’s data type will be Integer by default.

To make id field as UUID, there is a new field type UUIDField which was added in django version 1.8+.


import uuid
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()

>>> eventobject = Event.objects.all()
>>> eventobject.first().id