Implementing Custom User Models

Tutorial 4 of 5

Implementing Custom User Models in Django

Introduction

In this tutorial, we are going to learn how to implement custom user models in Django by replacing Django's default user model with our own.

By the end of this tutorial, you will know how to:
1. Create a custom user model
2. Replace Django's default user model with your own custom model

Prerequisites:
- Basic knowledge of Python
- A working understanding of Django

Step-by-Step Guide

Django's built-in User model is quite robust, but there may be instances where you want to implement custom behavior. Fortunately, Django allows us to extend or completely replace the built-in User model.

Creating a Custom User Model

Let's start by creating a new application named users. In your terminal, type:

python manage.py startapp users

In the users/models.py file, we'll create our custom user model.

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

class CustomUser(AbstractUser):
    # Add additional fields in here
    about = models.TextField(blank=True)

This CustomUser model will have all the fields of the built-in User model and an additional field about.

Replacing Django's Default User Model

To replace Django's default user model, we need to update the AUTH_USER_MODEL setting in our settings file (settings.py).

AUTH_USER_MODEL = 'users.CustomUser'

This tells Django to use our CustomUser model instead of the built-in User model.

Code Examples

  1. Adding more complex fields to the custom user model:
from django.contrib.auth.models import AbstractUser
from django.db import models

class CustomUser(AbstractUser):
    # Add more complex fields
    about = models.TextField(blank=True)
    birthdate = models.DateField(null=True, blank=True)

In this example, we have added a birthdate field to our CustomUser model. This field is optional (null=True, blank=True).

  1. Changing the string representation of the model:
from django.contrib.auth.models import AbstractUser
from django.db import models

class CustomUser(AbstractUser):
    about = models.TextField(blank=True)

    def __str__(self):
        return self.email

In this example, we have overridden the __str__() method to return the user's email instead of their username.

Summary

In this tutorial, you've learned how to create a custom user model and replace Django's default user model. To continue learning, you could explore adding more complex fields to your user model, or how to customize the user model's admin interface.

Practice Exercises

  1. Add a phone_number field to the CustomUser model. This field should be unique and optional.
  2. Override the get_full_name() method of the CustomUser model to return the user's email.

Solutions:
1. Adding a phone_number field:

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

class CustomUser(AbstractUser):
    about = models.TextField(blank=True)
    phone_number = models.CharField(max_length=20, unique=True, null=True, blank=True)
  1. Overriding the get_full_name() method:
from django.contrib.auth.models import AbstractUser
from django.db import models

class CustomUser(AbstractUser):
    about = models.TextField(blank=True)

    def get_full_name(self):
        return self.email

In this solution, the get_full_name() method returns the user's email instead of their full name.