Django Generate Unique Id

Published 26 Jun, 2020

Django Generate Unique Id or String for your application. Django UUID generate very unique primary key.

When comes to unique key in database table we have to use auto generated key. But in some cases we need to use custom generated key. The places like in url or any other external facing application. One of the best way to generate unique key is with UUID.

This is the best way user can generate unique key. And Django also support this. All you have to do is import uuid and create UUIDField in

import uuid
from django.db import models

class MyUUIDModel(models.Model):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    # other fields

After this you can forget about create unique key because Django automatically take care of it.

Just save the object and then the id will be uuid.

uuid_obj = MyUUIDModel.objects.create()

This article published under development on django tags. Please subscribe below forms to receive advertisement free articles.

Join the Newsletter

Subscribe to get our latest content by email.
    We won't send you spam. Unsubscribe at any time.