auth.User.groups: (fields.E304) Reverse accessor for 'User.groups' clashes with reverse accessor for 'UserManage.groups'

PythonDjango

Python Problem Overview


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'.

Python Solutions


Solution 1 - Python

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

Solution 2 - Python

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

Solution 3 - Python

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

AUTH_USER_MODEL="users.CustomUser"

Solution 4 - Python

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

Solution 5 - Python

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',
    },
]

Solution 6 - Python

I have added AUTH_USER_MODEL = 'YourAppName.YourClassName' to setting but still had an other error during on python manage.py migrate

Traceback (most recent call last):
  File "D:\Perso Work\Mr CYRILE\PUSH NOTIFICATION MOBILE AP\.env\lib\site-packages\django\core\management\base.py", line 398, 
in execute
    output = self.handle(*args, **options)
  File "D:\Perso Work\Mr CYRILE\PUSH NOTIFICATION MOBILE AP\.env\lib\site-packages\django\core\management\base.py", line 89, 
in wrapped    
res = handle_func(*args, **kwargs)  File "D:\Perso Work\Mr CYRILE\PUSH NOTIFICATION MOBILE AP\.env\lib\site-packages\django\core\management\commands\migrate.py", line 202, 
in handle    
pre_migrate_apps = pre_migrate_state.apps  File "D:\Perso Work\Mr CYRILE\PUSH NOTIFICATION MOBILE AP\.env\lib\site-packages\django\utils\functional.py", line 48, 
in __get__    

res = instance.__dict__[self.name] = self.func(instance)  File "D:\Perso Work\Mr CYRILE\PUSH NOTIFICATION MOBILE AP\.env\lib\site-packages\django\db\migrations\state.py", line 208, 
in apps    return StateApps(self.real_apps, self.models)  File "D:\Perso Work\Mr CYRILE\PUSH NOTIFICATION MOBILE AP\.env\lib\site-packages\django\db\migrations\state.py", line 277, 
in __init__    
raise ValueError("\n".join(error.msg for error in errors))ValueError: The field admin.LogEntry.user was declared with a lazy reference to 'reviews.employee', but app 'reviews' doesn't provide model 'employee'.
The field reviews.Review.user was declared with a lazy reference to 'reviews.employee', but app 'reviews' doesn't provide model 'employee'.

Solution 7 - Python

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

Solution 8 - Python

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionaircraftView Question on Stackoverflow
Solution 1 - PythonaircraftView Answer on Stackoverflow
Solution 2 - PythonAjayShelarView Answer on Stackoverflow
Solution 3 - PythonArnab DasView Answer on Stackoverflow
Solution 4 - PythonRavindrakumaraView Answer on Stackoverflow
Solution 5 - PythonTanuja JoshiView Answer on Stackoverflow
Solution 6 - PythonGreko2015 GuFnView Answer on Stackoverflow
Solution 7 - PythonHarshit SahuView Answer on Stackoverflow
Solution 8 - PythonLeo GeoView Answer on Stackoverflow