重写用户模型时报错AttributeError: type object ‘自定义类’ has no attribute ‘USERNAME_FIELD’

view中导入:from django.contrib.auth.models import AbstractBaseUser

settings.py中设置了:AUTH_USER_MODEL='app名.model自定义类'

解决方法:在自定义类中加:

  identifier = models.CharField(max_length=40, unique=True)
  USERNAME_FIELD = 'identifier'

view中导入from django.contrib.auth.models import AbstractUser 则不会出现该问题

原文地址:https://www.cnblogs.com/beiji/p/9670956.html