Summary: In this tutorial, you will learn the difference between AbstractUser and AbstractBaseUser classes in Django.

What are AbstractUser and AbstractBaseUser?

AbstractUser and AbstractBaseUser are both classes provided by Django to help developers create custom user models, but they serve slightly different purposes.

AbstractUser is a class that extends the built-in django.contrib.auth.models.User model. It provides additional fields and methods that are commonly used in user models, such as first and last name, email address, and date of birth.

It also provides methods for checking permissions and authentication. Developers can subclass AbstractUser and add or override fields and methods as necessary.

AbstractBaseUser is an abstract base class that provides the basic structure for creating a custom user model. It contains fields and methods that are commonly used in user models, such as username, password, email, is_active, and is_staff.

It also provides methods for checking permissions and authentication. Developers can subclass AbstractBaseUser and add or override fields and methods as necessary.

What are the differences between them?

The main difference between AbstractUser and AbstractBaseUser is that AbstractUser is an extension of the built-in django.contrib.auth.models.User model, while AbstractBaseUser is a standalone class that provides the basic structure for creating a custom user model.

Here’s an example of how you might create a custom user model using AbstractUser:

from django.contrib.auth.models import AbstractUser

class MyUser(AbstractUser):
    age = models.IntegerField()
    phone_number = models.CharField(max_length=15)
    is_premium = models.BooleanField(default=False)

In this example, MyUser subclasses AbstractUser, which is an extension of the built-in User model.

It has fields such as username, password, email, first and last name, which are inherited from the built-in User model, and also the additional fields such as age, phone_number, and is_premium.

Here’s an example of how you might create a custom user model using AbstractBaseUser:

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

class MyUser(AbstractBaseUser):
    email = models.EmailField(unique=True)
    is_active = models.BooleanField(default=True)
    is_premium = models.BooleanField(default=False)

    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = []

    objects = MyUserManager()

In this example, MyUser subclasses AbstractBaseUser, which is a standalone class that provides the basic structure for creating a custom user model. It has fields such as email, is_active and is_premium, and does not inherit the fields from built-in User model.

As you can see, AbstractUser provides more fields by default and already have the built-in fields, such as username, first_name, last_name and email, while AbstractBaseUser is a more minimal class that provides only the basic fields such as username and password and developer need to add fields and methods as per requirement.

Here, the MyUserManager is the custom implementation of BaseUserManager.

Refer to this post to know how to use BaseUserManager and AbstractBaseUser in Django:

Which to use When?

You can use AbstractUser if you want to start with a more feature-rich class, or use AbstractBaseUser if you want to start with a minimal class and build a custom user model from scratch.

In summary, AbstractUser is a more feature-rich class that is based on the built-in User model, while AbstractBaseUser is a more minimal class that provides the basic structure for creating a custom user model.

Developers can choose to use AbstractUser if they want to start with a more feature-rich class, or use AbstractBaseUser if they want to start with a minimal class and build a custom user model from scratch.

Post author avatar

Adarsh Kumar

I am an engineer by education and writer by passion. I started this blog to share my little programming wisdom with other programmers out there. Hope it helps you.

Leave a Reply