148

In my Django project I have a user_manage app.

I create a model named UserManage in my user_manage app's model.py:

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

class UserManage(AbstractUser):
    username = models.CharField(max_length=12)

Then I run:

$ python3 manage.py makemigrations

There comes the error:

ERRORS:
auth.User.groups: (fields.E304) Reverse accessor for 'User.groups' clashes with reverse accessor for 'UserManage.groups'.
        HINT: Add or change a related_name argument to the definition for 'User.groups' or 'UserManage.groups'.
auth.User.user_permissions: (fields.E304) Reverse accessor for 'User.user_permissions' clashes with reverse accessor for 'UserManage.user_permissions'.
        HINT: Add or change a related_name argument to the definition for 'User.user_permissions' or 'UserManage.user_permissions'.
users_management.UserManage.groups: (fields.E304) Reverse accessor for 'UserManage.groups' clashes with reverse accessor for 'User.groups'.
        HINT: Add or change a related_name argument to the definition for 'UserManage.groups' or 'User.groups'.
users_management.UserManage.user_permissions: (fields.E304) Reverse accessor for 'UserManage.user_permissions' clashes with reverse accessor for 'User.user_permissions'.
        HINT: Add or change a related_name argument to the definition for 'UserManage.user_permissions' or 'User.user_permissions'.
aircraft
  • 25,146
  • 28
  • 91
  • 166

7 Answers7

298

Add the following to settings.py:

AUTH_USER_MODEL = "users_management.UserManage" 

More generally,

AUTH_USER_MODEL = 'YourAppName.YourClassName'
  • YourAppName: This is the name of the app that will have the User Model
  • YourClassName: This is the name of the class used inside the models.py file
eykanal
  • 26,437
  • 19
  • 82
  • 113
aircraft
  • 25,146
  • 28
  • 91
  • 166
  • 1
    How can I add user from path like UsersManager.models.custom_user.User? – Ashen One Nov 19 '19 at 10:35
  • 2
    What if I need migrate first? ie: ... makemigrations users? Without AUTH_USER_MODEL I will receive the "clash" error. With AUTH_USER_MODEL: "AUTH_USER_MODEL refers to model '%s' that has not been installed" % settings.AUTH_USER_MODEL – mirek Apr 01 '20 at 08:40
  • To my previous comment: The reason was that inside same application with AUTH_USER_MODEL was a second model with use of get_user_model(). It was neccessary to comment out the 2nd model and make "makemigrations" in two steps. – mirek Apr 01 '20 at 09:34
  • I got this error `django.db.utils.ProgrammingError: relation "user_usertable" does not exist` with `user` is my app name and `UserTable` is the class inside my model – Chau Loi Aug 18 '21 at 03:56
30

Add this in the settings :

AUTH_USER_MODEL = 'APPNAME.User'

This way we are telling Django to use our custom model instead the default one. https://docs.djangoproject.com/en/2.2/topics/auth/customizing/#substituting-a-custom-user-model

AjayShelar
  • 462
  • 4
  • 6
  • 3
    Please edit your answer to add an explanation of how your code works and how it solves the OP's problem. Many StackOverflow users are newbies and will not understand the code you have posted, so will not learn from your answer. – i alarmed alien Oct 28 '18 at 14:39
9

Add this setting.py AUTH_USER_MODEL = "myapp.User"

Ravindrakumara
  • 131
  • 1
  • 4
  • 4
    The answer is identical to the already accepted one: https://stackoverflow.com/a/49189522/5537876 – RaideR Feb 20 '21 at 18:52
7

Add this in the settings at the end of the code :

AUTH_USER_MODEL="users.CustomUser"
Arnab Das
  • 149
  • 3
  • 3
7

The solution is to first add the following line into your settings.py-

AUTH_USER_MODEL="myproject.User"

Where myproject is your project name. If you again get error then run following commands in your main directory-

python manage.py makemigrations
python manage.py migrate

This worked for me

Harshit Sahu
  • 71
  • 1
  • 2
2

Just add AUTH_USER_MODEL="your app name.User" in settings.py as shown in the code below

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}
AUTH_USER_MODEL="myproject.User"
    
# Password validation
# https://docs.djangoproject.com/en/2.2/ref/settings/#auth-password-validators
    
AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]
0

If above steps don´t work, delete all migrations history and your database.

Then "makemigrations" and "migrate" like it was for the first time.

The User model must be created at beginning of the project, before firts migration.

Leo Geo
  • 9
  • 1