Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django user model and custom primary key field

Tags:

python

django

Django by default makes a primary key field on each model named "id", with a type of AutoField. On my models, I'm overriding this to use a custom UUIDField as the primary key by using the "primary_key" attribute. I would also like the User model in django.contrib.auth to have a UUIDField as the primary key, but this doesn't seem to be possible without changing the User model source code.

Is there any recommended way to approach this problem?

like image 475
Andrew Avatar asked Sep 04 '10 08:09

Andrew


1 Answers

One could leverage just the AbstractUser and make the following changes

class CustomUser(AbstractUser):
    email = models.EmailField(max_length=254, unique=True, db_index=True, primary_key=True)
    name = models.CharField(max_length=50)

    USERNAME_FIELD = 'email'
    REQUIRED_FILEDS = ['name']
like image 88
Raghotham S Avatar answered Oct 19 '22 12:10

Raghotham S