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.
users
appMake sure you are inside your project directory.
python manage.py startapp users
Then, add the following to the models.py:
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/admin.py
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from .models import CustomUser
class CustomUserAdmin(UserAdmin):
model = CustomUser
admin.site.register(CustomUser, CustomUserAdmin)
settings.py
filesettings.py
add to INSTALLED_APPS
("users.apps.UsersConfig",
)AUTH_USER_MODEL = 'users.CustomUser'
line to the bottom of the setting.py
file.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 users.models import User
Run the following two commands in your terminal, from the root of your project:
find . -path "*/migrations/*.py" -not -name "__init__.py" -delete
find . -path "*/migrations/*.pyc" -delete
python manage.py makemigrations
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;
python manage.py migrate --fake
python manage.py runserver
This should be it. If you went through each step sequentially, your app should be using a Custom User Model. Congrats!
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.
OSIG - Ad
Automatically create beautiful OG images for your site.
Automatically create beautiful OG images for your site.
Ad