131👍
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.
14👍
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.
- [Django]-Where are the Assertion Methods list from Django TestCase?
- [Django]-Django "login() takes exactly 1 argument (2 given)" error
- [Django]-Django gunicorn sock file not created by wsgi
13👍
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'
- [Django]-Django: How to set a field to NULL?
- [Django]-How to make two django projects share the same database
- [Django]-How to loop over form field choices and display associated model instance fields
11👍
You can directly add the id
field as a UUIDField
in the Person
model. There is no need for a separate MyUUIDModel
.
I think you have confused it with the MyUUIDModel
used in the UUIDField example where the id
is a UUIDField
. You can just use the below code and it will use UUIDs
for id
.
import uuid
from django.db import models
class Person(models.Model):
...
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
- [Django]-Change a field in a Django REST Framework ModelSerializer based on the request type?
- [Django]-Django model manager objects.create where is the documentation?
- [Django]-How do you get PyPy, Django and PostgreSQL to work together?
2👍
To use UUID in Django for a new model see Django Docs.
However, if you want to use it for the existing model (with unique=True) having data corresponding to it, you will not be able to do it directly by the above documentation. It will create migration errors.
To do it without losing the data follow all the steps carefully of this Django Documentation.
- [Django]-What is the advantage of Class-Based views?
- [Django]-Trying to migrate in Django 1.9 — strange SQL error "django.db.utils.OperationalError: near ")": syntax error"
- [Django]-Django storages aws s3 delete file from model record
1👍
in model import uuid:
import uuid
in class model use:
class Article(TimeStampedModel):
uuid = models.UUIDField(editable=False, default=uuid.uuid4, unique=True)
user = models.ForeignKey(User, on_delete=models.SET_NULL, related_name='articles', null=True)
categories = models.ManyToManyField(ArticleCategory, blank=True)
title = models.CharField(max_length=500, null=True, blank=True)
body = RichTextUploadingField(config_name='portal_lobar_config')
image = models.ImageField(upload_to='article_images/', null=True, blank=True)
headline = models.BooleanField(default=True)
tags = models.ManyToManyField(ArticleTag, blank=True)
slug = AutoSlugField(max_length=500, populate_from='title', unique_with='created__month', null=True)
published = models.BooleanField(default=False)
published_at = models.DateTimeField(null=True, blank=True)
def __str__(self):
return self.title
class Meta:
ordering = ['-created']
- [Django]-Django Password Generator
- [Django]-How to use if/else condition on Django Templates?
- [Django]-How to set a Django model field's default value to a function call / callable (e.g., a date relative to the time of model object creation)
-3👍
You can use the UUIDField in Django to generate unique IDs for each object in your model. The UUIDField is a special field to store universally unique identifiers. It uses Python’s UUID class. UUID, Universal Unique Identifier, is a python library that helps in generating random objects of 128 bits as ids. It provides the uniqueness as it generates ids on the basis of time, Computer hardware (MAC etc.). Universally unique identifiers are a good alternative to AutoField for primary_key. The database will not generate the UUID for you, so it is recommended to use default. Here is an example of how you can use 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 = models.ForeignKey(MyUUIDModel)
This should work for you. If you still face issues with generating unique IDs every time you create an object, you can try using uuid.uuid1() instead of uuid.uuid4(). The uuid1() function generates a UUID from the host ID, sequence number, and the current time.
- [Django]-How to upload a file in Django?
- [Django]-How do I force Django to ignore any caches and reload data?
- [Django]-Django: best practice way to get model from an instance of that model