Custom User Model in Django Tutorial 🎯

beginner
10 min

Custom User Model in Django Tutorial 🎯

Welcome to our comprehensive guide on creating a Custom User Model in Django! By the end of this tutorial, you'll understand how to customize the default user model to better suit your project's needs. πŸ“ Note: This guide is suitable for both beginners and intermediates.

Table of Contents πŸ“

  1. Understanding the Default Django User Model
  2. Creating a Custom User Model
  3. Advanced Custom User Model Examples

Understanding the Default Django User Model πŸ“

Django comes with a built-in user model AuthUser (derived from AbstractBaseUser and PermissionsMixin) which handles authentication and authorization. However, it may not always fit your application's requirements.


Creating a Custom User Model 🎯

To create a custom user model, we'll follow these steps:

  1. Create a new Django app (if not already created)
  2. Create a new user model class
  3. Register the custom user model with Django's authentication system
  4. Update the login view to use the new user model

Steps to Create a Custom User Model πŸ“

Let's call our custom user model MyUser.

  1. Create a new Django app:
bash
python manage.py startapp custom_users
  1. In the custom_users/models.py, create the MyUser class:
python
from django.contrib.auth.models import AbstractBaseUser, BaseUserManager class MyUserManager(BaseUserManager): pass class MyUser(AbstractBaseUser): email = models.EmailField(verbose_name='email', max_length=255, unique=True) is_active = models.BooleanField(default=True) is_staff = models.BooleanField(default=False) date_joined = models.DateTimeField(default=timezone.now) objects = MyUserManager() USERNAME_FIELD = 'email' REQUIRED_FIELDS = ['is_staff']
  1. Register the custom user model:
python
# custom_users/models.py from django.contrib.auth import get_user_model class CustomUser(get_user_model()): pass
  1. In the settings.py of your project, update AUTH_USER_MODEL:
python
AUTH_USER_MODEL = 'custom_users.CustomUser'
  1. Update the login view:
python
# custom_users/views.py from django.contrib.auth import login, authenticate from django.shortcuts import render, redirect from django.contrib.auth.forms import AuthenticationForm def login_view(request): if request.method == 'POST': form = AuthenticationForm(data=request.POST) if form.is_valid(): user = form.get_user() login(request, user) next_url = request.GET.get('next') if next_url: return redirect(next_url) else: return redirect('home') else: form = AuthenticationForm() return render(request, 'login.html', {'form': form})
Quick Quiz
Question 1 of 1

What is the name of the custom user model we created in this tutorial?


Advanced Custom User Model Examples 🎯

Adding Custom Fields πŸ“

You can add custom fields to your user model, such as first_name, last_name, age, etc. Just create a new field and add it to the MyUser class.

Overriding User Model Methods πŸ’‘ Pro Tip:

Django allows you to override user model methods like get_full_name(), get_short_name(), and more to better fit your application's needs.


Now that you've learned how to create a custom user model in Django, you're one step closer to building a robust web application! Happy coding! πŸ’‘ Pro Tip: Don't forget to test your custom user model thoroughly to ensure everything works as expected.