70👍
✅
If you’re using Django >= 1.8, you can use a UUIDField
:
import uuid
from django.db import models
class MyUUIDModel(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
Passing default = uuid.uuid4
auto-populates new records with a random UUID (but note that this will be done in Python code, not at the database level).
If you’re using an older version of Django, you can either upgrade, or use django-extensions
, which provides a UUIDField
as well.
Source:stackexchange.com