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 use 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 settings.py file

2- Create a Custom User Model by using AbstractUser 

3- Create a Custom Form for UserCreationForm, UserChangeForm

4-  Edit Admin.py file

 

Let's apply these 4 steps

1- At the bottom of the settings.py, 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 Models.py (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):
    pass
    #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 forms.py 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 admin.py 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']

admin.site.register(MyCustomUser, MyCustomUserAdmin)

 

 

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

python3 manage.py makemigrations

python3 manage.py migrate

python3 manage.py 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.

 

 

© selimatmaca.com. All Rights Reserved.