How to automatically create uuid column with django

I use django to create database tables for mysql and I want it to be able to create a column whose type is uuid, I hope that it can generate uuid by itself, which means that I need to insert a record every time, I do not need to specify uuid for the model object. How can I do this, thanks!

+4
source share
1 answer

If you use Django> = 1.8, you can useUUIDField :

import uuid
from django.db import models

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

The transfer default = uuid.uuid4automatically fills new entries with a random UUID (but note that this will be done in Python code, not at the database level).


Django, , django-extensions, UUIDField.

+12

Source: https://habr.com/ru/post/1619845/


All Articles