Istanbul/Turkey

5- 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. 

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:

We are telling Django that MyCustomUser model will be used for user authentication.

AUTH_USER_MODEL = 'moviesapp.MyCustomUser'

moviesapp 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 It is recommendedto name it as User like this:

AUTH_USER_MODEL = 'moviesapp.User'

 

2- Edit Models.py in moviesapp and create a new model named MyCustomUser (I added 2 additional fields dateofbirth 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
    dateofbirth = 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', 'dateofbirth', '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

 

 

Run the command below. This will copy all files from your static folders into the STATIC_ROOT directory.

python3 manage.py collectstatic

 

Restart apache service

sudo service apache2 restart 

 

Go to admin panel (http://prjmovies.local/admin in my case) logon with the super user and create a new user. Click on that user and you will see two additional fields (birthdate and sex) for your new user. In the following articles, we will be inserting data into birthdate and sex fields.

 

 That's it. We just succesfully extended our user model.

  • Hits: 1126