Yuwen Yan
Yuwen Yan

Reputation: 4935

Change the type of User ID to UUID

I'm using User model from django.contrib.auth.models, the default id(primary_key) type is int, how to change it to UUID?, for example id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)

Upvotes: 25

Views: 15467

Answers (2)

Ayman
Ayman

Reputation: 51

If you had already executed any migrations before making the UUID change, it is crucial to delete the tables and recreate the migrations.

Upvotes: 0

Aswin Murugesh
Aswin Murugesh

Reputation: 11070

Use AbstractUser model if you need changes in the default user model.

import uuid
from django.db import models
from django.contrib.auth.models import AbstractUser

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

Then in your settings.py,

AUTH_USER_MODEL = 'myapp.MyUser'

Upvotes: 42

Related Questions