Yes you should.
There is almost no reason not to, and plenty of reasons why it’s the right choice.
A universally unique identifier (UUID) is a 128-bit number used to identify information in computer systems. When generated according to the standard methods, UUIDs are, for practical purposes, unique.
The default ID on a Django Model is a auto incremented integer eg 1,2,3,…. etc.
Here are the Django Docs on UUID fields and this is how you add the id to your model:
import uuid from django.db import models class MyAwesomeModel(models.Model): id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False) # other fields go here
There is negligible performance difference on using a UUID
There is almost no performance hit in using a UUID over a auto-incremented integer.
Although I will add a caveat here – if you are writing software were the ounce of performance you’ll get out of using auto-increments vs UUID’s is important to you, then you are probably beyond the level of this post and you probably know what you are doing™.
UUID can be made public, IDs shouldn’t be
I’ve been burned on this one. I used to think if a model would ever be made public when creating a model. For example – will I need to reference it in a url? For now I just assume that every model will be made public at some point.
Alas, it is very difficult to know if your model will be public a few months down the track.
Specifications and business needs can change and you don’t want people being able to guess the next record in the model by changing the incrementing the id in the URL.
For example consider a list of customers. Say you create a page that lists the customers details.
If you have a url like www.example.com/100 – this will list the 100th customer in your database. Now an enterprising young hacker might change that number to 101 (www.example.com/101) and suddenly he can see the 101st customer. This is a bad thing.
The above is much more difficult to do if your customer url is www.example.com/b112f6fd-93ed-4ea3-8642-3107259f59d0
Converting to UUID on a current model is difficult
You never know where your project will end up, so you might want to make previously private models, public.
Converting the auto-increment ID to a UUID is a pain. Like a real pain. You can’t just get it happening with a migration – all sorts of errors pop up. Down the track I might post about how to do it – but for now – consider it a world of pain. Did I mention it is a world of pain? Pain. And Flames. Lots of Flames.
I hope this has given you enough reason to make UUID’s your default ID in a model. There are some exceptions but I assume you know what you are doing™ if you choose to auto-increment ID after considering UUIDs.