我需要帮助。我正在 Django 中创建一个注册/登录应用程序,但在迁移 CustomUser 类时遇到一些问题。以下是错误:
SystemCheckError: System check identified some issues:
ERRORS:
auth.User.groups: (fields.E304) Reverse accessor 'Group.user_set' for 'auth.User.groups' clashes with reverse accessor for 'custom_auth.CustomUser.groups'.
HINT: Add or change a related_name argument to the definition for 'auth.User.groups' or 'custom_auth.CustomUser.groups'.
custom_auth.CustomUser.groups: (fields.E304) Reverse accessor 'Group.user_set' for 'custom_auth.CustomUser.groups' clashes with reverse accessor for 'auth.User.groups'.
我尝试添加一个相关名称,作为对此处类似问题的建议,但当我进行迁移时,它仍然会抛出相同的错误。
我的 models.py 的代码如下:
# custom_auth/models.py
from django.contrib.auth.models import AbstractUser
from django.db import models
# Create your models here.
class CustomUser(AbstractUser):
phone_number = models.CharField(max_length=15)
designation = models.CharField(max_length=100)
# Add a related_name to the user_permissions field
user_permissions = models.ManyToManyField(
to='auth.Permission',
related_name='custom_users',
blank=True,
)
您必须覆盖组并为其指定相关名称
custom_groups = models.ManyToManyField(
Group,
related_name='custom_users',
blank=True,
)
将其添加到您的自定义用户模型中