Python auth.User.groups:(fields.E304)“User.groups”的反向访问器与“UserManage.groups”的反向访问器冲突

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/49189402/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 19:01:32  来源:igfitidea点击:

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

pythondjango

提问by aircraft

In my Django project I have a user_manageapp.

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

I create a model named UserManagein my user_manageapp's model.py:

UserManage在我的user_manage应用程序的 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'.

回答by aircraft

Add the following to settings.py:

将以下内容添加到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.pyfile
  • YourAppName:这是将具有用户模型的应用程序的名称
  • YourClassName:这是在models.py文件中使用的类的名称

回答by AjayShelar

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

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