Serhat Teker
Posted on March 31, 2020
Intro
In Django whenever we create any new model, there is an ID
-or PK
, model field attached to it. The ID
field’s data type is integer
by default.
To make this integer id field UUID
, we can use UUIDField
model field, which was added in Django 1.8
.
UUID
A universally unique identifier (UUID) is a 128-bit number used to identify information in computer systems. They are commonly represented as 32 hexadecimal characters separated by four hyphens.
There are 5 versions of UUID:
- Version 1: date-time and MAC address
- Version 2: date-time and MAC address, DCE security version
- Versions 3: namespace name-based
- Version 4: random
- Version 5: namespace name-based
For more details go to : WIKI UUID
Which UUID Version To Choose
There are two different ways of generating an UUID.
1. If you just need a "unique ID", you want a version 1 or version 4:
- Version 1: This generates a unique ID based on a network card MAC address and a timer. These IDs are easy to predict (given one, I might be able to guess another one) and can be traced back to your network card. It's not recommended to create these.
- Version 4: These are generated from random (or pseudo-random) numbers. If you just need to generate a UUID, this is generally what you want.
2. If you need to always generate the "same" or "reproducible" UUID" from a given name, you want a version 3 or version 5. It means that if you feed that algorith with the same input, it will generate the same output.
Version 3: This generates a unique ID from an MD5 hash of a namespace and name. If you need backwards compatibility (with another system that generates UUIDs from names), use this.
Version 5: This generates a unique ID from an SHA-1 hash of a namespace and name. This is the preferred version.
Collision
The probability to find a duplicate within 103 trillion version-4 UUIDs is one in a billion.
It means that if you produce "every second" an UUID for 3,266,108
year, your collision chance is .000000001
.
Django Model
As we mentioned earlier, we will use UUIDField
model field and "version 4" UUID as the default value for the field.
So let's create a model:
#src/myapp/models.py
import uuid
from django.db import models
from django.utils.translation import ugettext_lazy as _
class Event(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
name = models.CharField(_("Name of Event"), blank=True, max_length=255)
Run migrations to activate the model:
$ python manage.py makemigrations
$ python manage.py migrate
Test the Model
In the root directory of your project, run to activate python shell:
$ python manage.py shell
Type below codes:
# import the model
>>> from src.myapp.models import Event
# create and save an event
>>> event = Event(name="FullMoon")
>>> event.save()
# get al objects from database
>>> obj = Event.objects.all()
# get the id of the one that we just created
>>> obj.first().id
UUID('35bfdc19-6ec2-4c41-b0a3-b50d07b30043')
>>> obj.first().pk
UUID('35bfdc19-6ec2-4c41-b0a3-b50d07b30043')
As you can see, our new model uses UUID as an ID. And our UUID is
35bfdc19-6ec2-4c41-b0a3-b50d07b30043
.
All done!
References:
Posted on March 31, 2020
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.