我有延伸的AbstractBaseUser自定義的用戶模型,以及我自己的用戶管理器(GenericUserManager):Dajngo自定義用戶錯誤
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天時間才梳理出本網站上的其他問題,但不幸的是,它沒有幫助。我會非常感激,有人可以幫我弄清楚這一點。
編輯:固定的縮進,但錯誤依然存在
功能必須是類 –
內你在哪裏調用你的get_by_natural_key?在views.py文件? –