当使用自定义用户继承django 的auth时,执行python manage.py makemigrations 时会报错误
class UserProfile(AbstractUser):
image = models.ImageField(upload_to='user/', max_length=200, verbose_name='用户头像', null=True, blank=True)
nick_name = models.CharField(max_length=20, verbose_name='用户昵称', null=True, blank=True)
birthday = models.DateTimeField(verbose_name='用户生日', null=True, blank=True)
gender = models.CharField(choices=(('girl', '女'), ('boy', '男')), max_length=10, verbose_name='用户性别',
default='girl')
address = models.CharField(max_length=200, verbose_name='用户地址', null=True, blank=True)
phone = models.CharField(max_length=11, verbose_name='用户手机', null=True, blank=True)
add_time = models.DateTimeField(default=datetime.now, verbose_name='添加时间')
def __str__(self):
return self.username
class Meta:
verbose_name = '用户信息'
verbose_name_plural = verbose_name
错误详情
auth.User.groups: (fields.E304) Reverse accessor 'Group.user_set' for 'auth.User.groups' clashes with reverse accessor for 'users.UserProfile.groups'.
HINT: Add or change a related_name argument to the definition for 'auth.User.groups' or 'users.UserProfile.groups'.
auth.User.user_permissions: (fields.E304) Reverse accessor 'Permission.user_set' for 'auth.User.user_permissions' clashes with reverse accessor for 'users.UserProfile.user_permissions'.
HINT: Add or change a related_name argument to the definition for 'auth.User.user_permissions' or 'users.UserProfile.user_permissions'.
users.UserProfile.groups: (fields.E304) Reverse accessor 'Group.user_set' for 'users.UserProfile.groups' clashes with reverse accessor for 'auth.User.groups'.
HINT: Add or change a related_name argument to the definition for 'users.UserProfile.groups' or 'auth.User.groups'.
users.UserProfile.user_permissions: (fields.E304) Reverse accessor 'Permission.user_set' for 'users.UserProfile.user_permissions' clashes with reverse accessor for 'auth.User.user_permissions'.
HINT: Add or change a related_name argument to the definition for 'users.UserProfile.user_permissions' or 'auth.User.user_permissions'.
解决方案
在项目的settings文件中加入
AUTH_USER_MODEL = 'users.UserProfile' # 其中user为app名称,User为模型类名称
重新执行python manage.py makemigrations python manage.py migrate