集成抽象类后出现的问题
rbac/modes.py/class User()
from django.contrib.auth.models import AbstractUser class User(AbstractUser): # 用户表 roles = models.ManyToManyField(to=Role, verbose_name="用户所拥有的权限", blank=True) def __str__(self): return self.username class Meta: abstract = True
业务类的model下
class UserInfo(User): # 员工表 name = models.CharField(max_length=32, unique=True, null=True, verbose_name="姓名") birthday = models.DateField(verbose_name="生日", blank=True) age = models.IntegerField(verbose_name="年龄", blank=True, null=True) gender = models.CharField(max_length=10, choices=(("Female", "女"), ("Male", "男")), verbose_name="性别", default="Male") image = models.ImageField(upload_to="image/%Y/%m", verbose_name="头像", null=True, blank=True) depart = models.ForeignKey("Department", on_delete=models.CASCADE, null=True, blank=True) USERNAME_FIELD = "username" def __str__(self): return self.name class Meta: verbose_name = "员工表" verbose_name_plural = "员工表"
继承了 AbstractUser 抽象类后,进行数据库迁移后提示错误
ERRORS: auth.User.groups: (fields.E304) Reverse accessor for 'User.groups' clashes with reverse accessor for 'UserInfo.groups'. HINT: Add or change a related_name argument to the definition for 'User.groups' or 'UserInfo.groups'. auth.User.user_permissions: (fields.E304) Reverse accessor for 'User.user_permissions' clashes with reverse accessor for 'UserI nfo.user_permissions'. HINT: Add or change a related_name argument to the definition for 'User.user_permissions' or 'UserInfo.user_permissions '. crm.ClassStudyRecord.course_memo: (fields.E120) CharFields must define a 'max_length' attribute. crm.UserInfo.groups: (fields.E304) Reverse accessor for 'UserInfo.groups' clashes with reverse accessor for 'User.groups'. HINT: Add or change a related_name argument to the definition for 'UserInfo.groups' or 'User.groups'. crm.UserInfo.user_permissions: (fields.E304) Reverse accessor for 'UserInfo.user_permissions' clashes with reverse accessor for 'User.user_permissions'. HINT: Add or change a related_name argument to the definition for 'UserInfo.user_permissions' or 'User.user_permissions '.
出现的错误原因是:
modes 没有继承原生的用户 authouser 用户认证组件了,所以django不知道你用的时候哪种用户认证表:
需要在settins.py 里面添加说明:
AUTH_USER_MODEL="你的业务app.UserInfo"
说明我需要用自己的这个用户认证表
普通人!