Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use the Custom User Model in Django 1.5

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

like image 769
Iqbal Avatar asked Feb 27 '13 22:02

Iqbal


People also ask

Should you create a custom user model in Django?

Every new Django project should use a custom user model. The official Django documentation says it is “highly recommended” but I'll go a step further and say without hesitation: You are straight up crazy not to use a custom user model up front.

Should I use default Django user model?

Whenever you are starting a new Django project, always swap the default user model. Even if the default implementation fit all your needs. You can simply extend the AbstractUser and change a single configuration on the settings module.

What is custom user model?

Custom user model manager where email is the unique identifiers. for authentication instead of usernames. """ def create_user(self, email, password, **extra_fields):

What you should know about the Django user model?

For Django's default user model, the user identifier is the username, for custom user models it is the field specified by USERNAME_FIELD (see Customizing Users and authentication). It also handles the default permissions model as defined for User and PermissionsMixin .


1 Answers

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'

like image 96
Dan Hoerst Avatar answered Sep 21 '22 23:09

Dan Hoerst