How to Create Custom User Model in Django

Learn how to extend Django's built-in User model by creating a custom user model with AbstractUser class.

September 10, 2024
4 min read
Updated Sep 10, 2024
django python authentication models

While building the backend for my web application using Django, I realized I needed a CustomUser model. Django’s built-in model is excellent, but I’m not a fan of using an auto-incrementing integer as a primary key. So, I decided to create my own custom user model to add some extra features, and fortunately, Django provides a great way to do this.

You might also consider doing this if you want to make the user email required or add extra fields like is_subscriber, etc.

In this article, I’ll show you how to extend the User model in Django using a custom model that extends the AbstractUser class.

By the way, if you want to add extra details to a user, like date of birth or phone number, you should create a completely separate model that links to the User model using a Foreign Key.

Extending the User Model using AbstractUser

First of all create an app named account

Terminal window
python3 manage.py startapp account

Now in account/models.py you can create custom user. Something like this:

account/models.py
from django.db import models
from django.contrib.auth.models import AbstractUser
import random
class MyUser(AbstractUser):
id = models.BigAutoField(
primary_key=True,
editable=False,
unique=True,
)
def save(self, *args, **kwargs):
if not self.id:
self.id = random.randint(10**14, 10**15-1)
super().save(*args, **kwargs)

In the above code I’m trying to make my user to have an unique id (primary key) of random 15 digit number.

Why 15 digits only?

Well, JavaScript can’t properly handle numbers larger than 15 digits, and if you’re building a web application, you’ll inevitably use JavaScript. There are ways to handle such large numbers in JavaScript, but I’m not doing that for a small project like this. A 15-digit number is already overkill for a user account ID — not a user message ID.

Why not UUID?

I’ve tried using UUID, but it’s a bit too costly for database operations. However, it is undoubtedly the best way to ensure your primary key is always unique. Integers make it much easier for the database to handle data.

Update your settings.py to support this new User Model:

settings.py
# .....
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'account.apps.AccountConfig'
]
# ....
# Custom User Model
AUTH_USER_MODEL = 'account.MyUser'

Now you can run the migration commands to update User Model with the custom MyUser Model.

If you already have some user data, it will show you an error. To solve this, you need to write your custom migration. I might write about this sometime.

Conclusion

Creating a custom user model in Django by extending the AbstractUser class is a powerful way to tailor the authentication system to your specific needs. Whether you want to use a different primary key, make email addresses mandatory, or add custom fields like is_subscriber, Django’s flexibility allows you to do so with relative ease.

Remember, implementing a custom user model early in your project is ideal, as it can be challenging to migrate an existing project with user data. With the steps outlined above, you should be well-equipped to create and manage your custom user model. By updating your settings.py and ensuring your models are correctly configured, you can integrate your custom user model seamlessly into your Django application.

Django is an incredibly powerful backend framework, packed with so many features that I could probably write a book on simple tips alone. I’ve kept this article concise and to the point. Good luck with your backend project, and happy coding!