2

I am trying to extend a User model and add some fields and below is my approach:

Class UserProfile(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    mobile_number = models.CharField(max_length=20)
    gender = models.CharField(max_length=2, choices=GENDER_CHOICES)
    location = models.ForeignKey(Location, blank=True, null=True)

class User_One(UserProfile):
    field_1 = models.CharField(max_length=20)
    ....
    ....

class User_Two(UserProfile):
    field_1 = models.CharField(max_length=20)
    ....
    ....

So basically there are two types of users User_One and User_Two and now whenever we save the two types of users into database the following happens

  1. A User model record will be created with separate id of values 1,2,3 etc.,
  2. A User_One model record will be created with id's 1,2,3
  3. A User_Two model record will be created with id's 1,2,3

So for each every model record saving, Django or database was generating id's 1,2,3.

But I got a requirement that User model should generate a uuid for id field value in place of integers, was that possible?

I mean something like below

class User_Profile(models.Model):
    id = models.IntegerField(default=uuid.uuid4)
Igonato
  • 10,175
  • 3
  • 35
  • 64
Shiva Krishna Bavandla
  • 25,548
  • 75
  • 193
  • 313
  • 1
    Answer on: http://stackoverflow.com/questions/3936182/using-a-uuid-as-a-primary-key-in-django-models-generic-relations-impact – Marin May 22 '17 at 07:14

1 Answers1

4

There are couple extra steps required to use uuid as a primary key:

  1. Use UUIDField for your id field instead of InegerField because uuid isn't exactly an integer
  2. Specify primary_key=True for that field

To get the custom user model, subclass it form django.contrib.auth.models.AbstractUser and specify AUTH_USER_MODEL in your settings:

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

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

Then in your settings file:

AUTH_USER_MODEL = 'youruserapp.UserProfile'

It's important to do that before you have any migrations (created a database) or this won't work.

Igonato
  • 10,175
  • 3
  • 35
  • 64
  • can we generate this `uuid` in place of id value for standard `User` model id field ? – Shiva Krishna Bavandla May 22 '17 at 07:18
  • @shivakrishna Yes, absolutely. You need a [custom user model](https://docs.djangoproject.com/en/1.11/topics/auth/customizing/#using-a-custom-user-model-when-starting-a-project) – Igonato May 22 '17 at 07:20
  • do we need to use any custom UserManager for the above user model ? – Shiva Krishna Bavandla May 22 '17 at 07:28
  • awesome, so now we can inherit this `User_Profile` model and can create any number models like `User_one(User_Profile), User_two(User_Profile)` etc., – Shiva Krishna Bavandla May 22 '17 at 07:32
  • @shivakrishna Yes, it should work.I don't know your exact requirements, you might want to use [proxy inheritance](https://docs.djangoproject.com/en/1.11/topics/db/models/#proxy-models) if you want to keep it a single table in the database. – Igonato May 22 '17 at 07:39
  • No, i want to create separate models for each class, ok now if we delete the `User_Profile` model, all the child classes(models) that inherited from this `User_Profile` model will also be deleted ? – Shiva Krishna Bavandla May 22 '17 at 07:42
  • I would suggest asking a new question specifically about inheritance if you end up facing issues with it – Igonato May 22 '17 at 07:42
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/144811/discussion-between-igonato-and-shiva-krishna). – Igonato May 22 '17 at 07:44