auth.User.groups:(fields.E304)' User.groups'的反向访问者与UserManage.groups' UserManage.groups'的反向访问者冲突

时间:2018-03-09 08:21:17

标签: python django

在我的Django项目中,我有一个user_manage应用程序。

我在UserManage应用的model.py中创建了一个名为user_manage的模型:

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

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

然后我跑:

$ python3 manage.py makemigrations

出现错误:

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

5 个答案:

答案 0 :(得分:32)

感谢@ABDULNIYASPM

我添加:

AUTH_USER_MODEL = "users_management.UserManage" 

settings.py中。

模板是

AUTH_USER_MODEL ='YourAppName.YourClassName>'

YourAppName :这是具有用户模型的应用的名称 YourClassName :这是 models.py 文件中使用的类的名称

答案 1 :(得分:7)

在设置中添加它:

AUTH_USER_MODEL = 'APPNAME.User'

这样,我们告诉Django使用自定义模型而不是默认模型。 https://docs.djangoproject.com/en/2.2/topics/auth/customizing/#substituting-a-custom-user-model

答案 2 :(得分:2)

在代码末尾的设置中添加:

AUTH_USER_MODEL="users.CustomUser"

答案 3 :(得分:1)

只需在AUTH_USER_MODEL="your app name.User"中添加settings.py,如下面的代码所示

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

答案 4 :(得分:-1)

添加这个设置.py AUTH_USER_MODEL = "myapp.User"