我有一个扩展 AbstractBaseUser 的自定义用户模型,以及我自己的用户管理器 (GenericUserManager):
class GenericUserManager(BaseUserManager):
def create_user(self, username, email, password, key_expires):
if not email:
raise ValueError('Users must have an email address')
if not username:
raise ValueError("users must have a username")
if not password:
raise ValueError('users must have password')
user = self.model(
username=username,
email=self.normalize_email(email),
key_expires=key_expires,
)
user.set_password(password)
user.save(using=self._db)
return user
def get_by_natural_key(self, username):
return self.get(username=username)
class BaseRegistrationUser(AbstractBaseUser):
username = models.CharField(max_length=150, unique=True)
email = models.EmailField(
verbose_name='email address',
max_length=255,
unique=False,
)
activation_key = models.CharField(max_length=90)
key_expires = models.DateTimeField()
is_active = models.BooleanField(default=False)
is_admin = models.BooleanField(default=False)
date_joined = models.DateTimeField(('date joined'),
default=datetime.datetime.now)
is_merchant_or_customer = models.CharField(max_length=20, null=False)
USERNAME_FIELD = 'username'
REQUIRED_FIELDS = ['username, email']
def __str__(self):
return self.username
...
def natural_key(self):
return (self.email,)
...
class Customer(BaseRegistrationUser):
first_name = models.CharField(max_length=150, default="", null=True)
last_name = models.CharField(max_length=150, default="", null=True)
slug = models.SlugField(max_length=175, default="", null=True)
objects = GenericUserManager()
...
这些可以很好地用于注册目的,但是,每当我尝试登录“客户”用户时,我都会收到以下错误:
File "C:\Users\OEM\Documents\repos\repo\ecommerce\myapp\views.py" in get_profile
126. user = authenticate(username=username, password=password)
File "C:\Users\OEM\AppData\Local\Programs\Python\Python36-32\lib\site-
packages\django\contrib\auth\__init__.py" in authenticate
70. user = _authenticate_with_backend(backend, backend_path,
request, credentials)
File "C:\Users\OEM\AppData\Local\Programs\Python\Python36-32\lib\site-
packages\django\contrib\auth\__init__.py" in _authenticate_with_backend
115. return backend.authenticate(*args, **credentials)
File "C:\Users\OEM\AppData\Local\Programs\Python\Python36-32\lib\site-
packages\django\contrib\auth\backends.py" in authenticate
18. user =
UserModel._default_manager.get_by_natural_key(username)
Exception Type: AttributeError at /get-profile/
Exception Value: 'Manager' object has no attribute 'get_by_natural_key'
我不知道是什么原因造成的,因为“get_by_natural_key”属性是在我的 UserManager 中定义的。
我已经尝试过新的数据库迁移,并花了整整 3 天的时间来梳理该站点上的其他问题,但不幸的是,它没有帮助。我将非常感谢有人可以帮助我解决这个问题。
编辑:修复了缩进,但错误仍然存在