When to use the Custom User Model in Django 1.5
Asked Answered
V

1

14

I have a question regarding the custom user model in Django 1.5

So right now the default user model looks just fine to me, I just need to add a few other variables such as gender,location and birthday so that users can fill up those variables after they have successfully registered and activated their account.

So, what is the best way to implement this scenario?

Do I have to create a new app called Profile and inherit AbstractBaseUser? and add my custom variable to models.py? Any good example for me to follow?

thank you in advance

Vassily answered 27/2, 2013 at 22:19 Comment(1)
If you are not worring about database optimization you can create additional profile model and make one-to-one relation to the User model from django.contrib.auth.models. The changes in Django 1.5 allows you create custom user model, in this case the model corresponds to 1 database table. It's not necessary to create a special separated application, you can add the model to some common models.py. But if you want, you can.Sudhir
O
28

You want to extend your user model to the AbstractUser and add your additional fields. AbstractUser inherits all of the standard user profile fields, whereas AbstractBaseUser starts you from scratch without any of those fields.

It's hard to define best practices this close to the release, but it seems that unless you need to drastically redefine the User model, then you should use AbstractUser where possible.

Here are the docs for extending the User model using AbstractUser

Your models.py would then look something like this:

class MyUser(AbstractUser):
    gender = models.DateField()
    location = models.CharField()
    birthday = models.CharField()

MyUser will then have the standard email, password, username, etc fields that come with the User model, and your three additional fields above.

Then you need to add the AUTH_USER_MODEL to your settings.py:

AUTH_USER_MODEL = 'myapp.MyUser'

Osmen answered 27/2, 2013 at 22:30 Comment(4)
Nice answer, haven't got around the new method yet – pinboarded. :-)Nine
while i creating a user i got this error :Manager isn't available; User has been swapped for 'myapp.MyUser'Rance
#suhail. What you need is to provide a custom manager for your custom user model.Wilds
what if if extends like this class MyUser(AbstractBaseUser, PermissionsMixin):Berey

© 2022 - 2024 — McMap. All rights reserved.