Django 2.0

How to Extend Existing User Model in Django

It is recommended to extend the existing user model every time we start a new project. Even if we think the standard user model is sufficient for our current project, in time we might need to have additional fields such as user birthday, user divison or user department etc.

IMPORTANT: Extending the user model should be done before we run makemigrations and migrate commands.

Before running makemigrations and migrate commands of a new project, we should implement the following 4 steps to extend the standard user.

1- Edit file

2- Create a Custom User Model by using AbstractUser

3- Create a Custom Form for UserCreationForm, UserChangeForm

4- Edit file

Let’s apply these 4 steps

1- At the bottom of the, add this:

AUTH_USER_MODEL = 'crmapp.MyCustomUser'

crmapp is my app’s name. In this example, I name the new custom user as MyCustomUser. You can name it whatever you want but in production environment I would just name it as User like this:

AUTH_USER_MODEL = 'crmapp.User'

2- Edit (I added 2 additional fields birth_date and sex). As I said, In a real project do not name the new custom user as MyUser or CustomUser. Name it just User. I name it MyCustomUser here to prevent confusion in this example.

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

class MyCustomUser(AbstractUser):
    #add additional fields you need here
    birth_date = models.DateField(blank=True, null=True)
    sex = models.CharField(max_length=10, blank=True, null=True)

    def __str__(self):
        return self.username

3- Create and Edit in your app and create 2 new forms. One inherits from UserCreationForm and the other from UserChangeForm.

from django import forms
from django.contrib.auth.forms import UserCreationForm, UserChangeForm
from .models import MyCustomUser

class MyCustomUserCreationForm(UserCreationForm):

    class Meta(UserCreationForm):
        model = MyCustomUser
        fields = ('username', 'email')

class MyCustomUserChangeForm(UserChangeForm):

    class Meta(UserChangeForm):
        model = MyCustomUser
        fields = ('username', 'email')

4- Edit file. This is for admin panel.

the add_form is used when you construct a new CustomUser, whereas the simple form is used to change data for an existing CustomerUser object.

from django.contrib import admin
from django.contrib.auth import get_user_model
from django.contrib.auth.admin import UserAdmin

from .forms import MyCustomUserCreationForm, MyCustomUserChangeForm
from .models import MyCustomUser

class MyCustomUserAdmin(UserAdmin):
    add_form = MyCustomUserCreationForm
    form = MyCustomUserChangeForm
    model = MyCustomUser
    list_display =['email', 'username', 'is_superuser', 'date_joined', 'birth_date', 'sex'], MyCustomUserAdmin)

Now you can run makemigrations and migrate commands. Then crate a super user and login.

python3 makemigrations

python3 migrate

python3 createsuperuser

Go to admin panel and create a new user. Click on that user and you will see two additional fields (birthdate and sex) for your new user.

Post Comment