Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use UUID

I am trying to get unique IDs for my Django objects. In Django 1.8 they have the UUIDField. I am unsure how to use this field in order to generate unique IDs for each object in my model.

Here is what I have for the UUIDField

import uuid
from django.db import models

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

class Person(models.Model):
    ...
    unique_id = MyUUIDModel()

I can reproduce the id for the UUID model, but everytime I do I get the exact same id. For Example:

person = Person.objects.get(some_field = some_thing)
id = person.unique_id.id

id then gives me the same id every time. What is wrong, how do I fix this?

like image 532
Chad Crowe Avatar asked Sep 11 '15 16:09

Chad Crowe


People also ask

How is UUID used?

UUIDs are generally used for identifying information that needs to be unique within a system or network thereof. Their uniqueness and low probability in being repeated makes them useful for being associative keys in databases and identifiers for physical hardware within an organization.

How do I read UUID?

Format. In its canonical textual representation, the 16 octets of a UUID are represented as 32 hexadecimal (base-16) digits, displayed in five groups separated by hyphens, in the form 8-4-4-4-12 for a total of 36 characters (32 hexadecimal characters and 4 hyphens). For example: 123e4567-e89b-12d3-a456-426614174000.

Should I use UUID?

Why use a UUID? 🔗 The main advantage of using UUIDs is that you can create a UUID and use it to identify something, such as a row in a database, with near certainty that the identifier will not exist in another row in your system or anyone else's.

What is the use of UUID in react?

UUID (Universal Unique Identifier) is a 16-byte unique value . You can use UUID as an identifier for objects. UUID is indexable and you can use it as a primary key.


3 Answers

I'm not sure why you've created a UUID model. You can add the uuid field directly to the Person model.

class Person(models.Model):
    unique_id = models.UUIDField(default=uuid.uuid4, editable=False, unique=True)

Each person should then have a unique id. If you wanted the uuid to be the primary key, you would do:

class Person(models.Model):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)

Your current code hasn't added a field to the person. It has created a MyUUIDModel instance when you do MyUUIDModel(), and saved it as a class attribute. It doesn't make sense to do that, the MyUUIDModel will be created each time the models.py loads. If you really wanted to use the MyUUIDModel, you could use a ForeignKey. Then each person would link to a different MyUUIDModel instance.

class Person(models.Model):
    ...
    unique_id = models.ForeignKey(MyUUIDModel, unique=True)

However, as I said earlier, the easiest approach is to add the UUID field directly to the person.

like image 195
Alasdair Avatar answered Oct 29 '22 12:10

Alasdair


You need to use the class you created as a subclass when declaring your Person model like this:

import uuid
from django.db import models

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

class Person(MyUUIDModel):
  ...

This way Person becomes a subclass of MyUUIDModel and will inherit its id field definition.

like image 25
Sylvestre Avatar answered Oct 29 '22 13:10

Sylvestre


EDIT: Actually I was wrong. It's not possible yet to implement it as DEFAULT_AUTO_FIELD as it has to inherit from IntegerField. Here's the ticket in the django project with feature request to make it possible. Once it's resolved I'll update my answer.


As of Django 3.2, if you want to use uuid as a pk for all your models on a project-wide level, you don't need a generic abstract model anymore. Just define DEFAULT_AUTO_FIELD setting

default value

DEFAULT_AUTO_FIELD = 'django.db.models.AutoField'

so something like this should work

DEFAULT_AUTO_FIELD = 'django.db.models.UUIDField'

Or even better, create your own field.

DEFAULT_AUTO_FIELD = 'project.common.models.CustomUUIDField'

Where you also define uuid type etc.

As seen in the docs, it can also be applied on an app level.

class MyAppConfig(AppConfig):
    default_auto_field = 'project.common.models.CustomUUIDField'
like image 12
Tom Wojcik Avatar answered Oct 29 '22 14:10

Tom Wojcik