[Django]-In Django, how to rename user model?

0👍

Just ran into this problem, here is how I handled it:

Assuming that your user models are in an app called "users":

Leave the AUTH_USER_MODEL setting at the old value (users.CustomUser).

In users/model.py, rename the class from CustomUser to User add a new class with the old name that inherits from the new name

class CustomUser(User):
    pass

Use manage.py to create an empty migration file

python manage.py makemigrations --empty users

Open the newly create migration file, and add this operation:

operations = [
    migrations.RenameModel('CustomUser', 'User')
]

Migrate the changes

python manage.py migrate users

Then you can go and change AUTH_USER_MODEL to users.User and remove the CustomUser class from users/model.py

I reference the user model using settings.AUTH_USER_MODEL when defining a ForeignKey and get_user_model() from django.contrib.auth anywhere else so I had no other references that needed to be changed.

👤Bill K

Leave a comment