back to tutorials

Migrating to a Custom User Model mid-project in Django

Whenever you are building a site with Django that will have user authentication, it is recommended to create a Custom User Model before the first migration. Sometimes you forget to do that. In this case, you have to follow a strict procedure, which I'll show you in the post.

This was Issue was discussed at length by the Django community. There is now a consensus about the best and the least painful way to do that. I'd like to take that discussion and summarize it into a set of actionable steps.

1. Create the users app

Make sure you are inside your project directory.

python startapp users

Then, add the following to the

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

class CustomUser(AbstractUser):
    class Meta:
        db_table = 'auth_user'

If you don't specify the name, you'll receive an error:

django.db.utils.OperationalError: no such table: users_customuser

Then, register the new Model in the admin panel:

# In users/
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from .models import CustomUser

class CustomUserAdmin(UserAdmin):
    model = CustomUser, CustomUserAdmin)

2. Update file

  • In add to INSTALLED_APPS ("users.apps.UsersConfig",)
  • Add AUTH_USER_MODEL = 'users.CustomUser' to the bottom.

3. Replace User imports

In your project code, replace all imports of the Django User model:

from django.contrib.auth.models import User

with the new, custom one:

from Accounts.models import User

4. Delete Old Migrations

Run the following to commands in your terminal, from the root of your project:

  1. find . -path "*/migrations/*.py" -not -name "" -delete
  2. find . -path "*/migrations/*.pyc" -delete

5. Create New Migrations

python makemigrations

6. Truncate (delete) contents of the migrations table

You will need to do this manually by going inside your database (Postgres, sqlite3, MySQL, etc.).

I was using sqlite3 at the time, so I had to do the following:

# login into the sqlite database
sqlite3 db.sqlite
# Then run the following
> DELETE FROM django_migrations;
> .quit

If you are using Postgres, you will have to first login into your database and then run:

TRUNCATE TABLE django_migrations;

7. Fake apply new migrations

python migrate --fake

8. Test

python runserver

This should be it. If you went through each step sequentially, your app should be using a Custom User Model. Congrats!

Bonus Video

If you prefer a more visual approach, I've made a video that shows how to migrate to a Custom User Model mid-project.

If you have any feedback, please let me know on Twitter. Your likes, retweets, and replies will show up here.


If you enjoyed this post, or found it useful, please consider signing up to my newsletter. Every week, I share my thoughts on cool stuff I found around the internet. No spam, ever. Unsubscribe any time.

12 Webmentions

Want to respond? Reply, like, reply or bookmark on Twitter :)