Django Part 2 - Creating a custom User model
Tags: django, python
Published: 2022-06-02 06:14:18

We are going to create a custom user class that will be used when users register an account and login. It is much easier to do this before running the initial database migrations, we can then easily make adjustments to the model later.

We will define the custom user model in the users/models.py file. The model will have all the default User model fields, such as username, email and password. On top of that, we are adding an optional name field and an email_verified field that will default to False and update to True when a user verifies their email.

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

class CustomUser(AbstractUser):
    name = models.CharField(max_length=100, null=True, blank=True)
    email_verified = models.BooleanField(default=False)
```

We need to update the settings.py file so that we can use the newly created CustomUser model for authentication.

```
AUTH_USER_MODEL = 'users.CustomUser'
```

Before we can start the server again, we need to run the database migrations. We use the manage.py script to create and run database migrations.

```
py manage.py makemigrations
py manage.py migrate
```

Django comes with some pre-made admin functionality that can be accessed at http://127.0.0.1:8000/admin

In order to log in there, we need to create a super user account which can be done using the manage.py script. 

```
py manage.py createsuperuser
```

Follow the instructions and enter your username, email and password, then navigate to the admin site at http://127.0.0.1:8000/admin

You can now login using the credentials you just created.
In the admin site, we can see all the database models and instances. 
Normally, we would be able to see the User model, however at the moment we can only see the Groups model because we created our own custom user model. In order to see the User model in the admin site, we are going to write a custom admin class.

Before we do that, we are going to create some forms that will be used by the admin class to enable us to create and update User instances.
Create a new file, forms.py, inside the users directory.
In users/forms.py:

```
from django.contrib.auth.forms import UserChangeForm, UserCreationForm
from .models import CustomUser

class CustomUserCreationForm(UserCreationForm):
    class meta(UserCreationForm):
        model = CustomUser
        fields = UserCreationForm.Meta.fields + ('name',)

class CustomUserChangeForm(UserChangeForm):
    class meta(UserChangeForm):
        model = CustomUser
        fields = UserChangeForm.Meta.fields
```

Then in the users/admin.py file, we will create the custom admin class and register it to the admin site.

```
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from .models import CustomUser
from .forms import CustomUserChangeForm, CustomUserCreationForm

class CustomUserAdmin(UserAdmin):
    add_form = CustomUserCreationForm
    form = CustomUserChangeForm
    model = CustomUser
    list_display = [
        'email',
        'username',
        'name',
        'is_staff',
        'email_verified'
    ]
    fieldsets = UserAdmin.fieldsets + ((None, {"fields": ("name",)}),) 
    add_fieldsets = UserAdmin.add_fieldsets + ((None, {"fields": ("name",)}),)

admin.site.register(CustomUser, CustomUserAdmin)
```

Now when logged into the admin site you can see the Users model and our user instance that we are logged in with.

In the next part of the tutorial, we will be creating the API endpoints to register, login and verify email address.