1
我正在學習試圖實現自定義用戶模型的django,但每次嘗試使用它登錄到/ admin時,我都會收到兩個長的回溯列表,在TypeError:get_session_auth_hash()缺少1個必需的位置參數:'self'
TypeError: get_session_auth_hash() missing 1 required positional argument: 'self'
,沒有在追蹤單個文件或電話的是我的文件
一個以防萬一,這裏是我的模型和後端文件
models.py
from django.db import models
from django.contrib.auth.models import BaseUserManager, AbstractBaseUser
class UserManager(BaseUserManager):
"""
Custom user manager
"""
def create_user(self, username, password):
if not username or not password:
raise ValueError("We need username and password here...")
user = self.model(username=username)
user.set_password(password)
user.save()
return user
def create_superuser(self, username, password):
if not username or not password:
raise ValueError("We need username and password here...")
user = self.create_user(username=username,password=password)
user.set_password(password)
user.is_admin = True
user.is_staff = True
user.save()
return user
class User(AbstractBaseUser):
"""
Custom user class
"""
username = models.CharField(max_length=64, unique=True, db_index=True)
joined = models.DateTimeField(auto_now_add=True, auto_now=False)
is_active = models.BooleanField(default=True)
is_staff = models.BooleanField(default=False)
is_admin = models.BooleanField(default=False)
objects = UserManager()
USERNAME_FIELD = 'username'
def __str__(self):
return self.username
Backends.py
from django.contrib.auth.models import check_password
from accounts.models import User
class AuthenticationBackend(object):
"""
Backend for authentication
"""
def authenticate(self, username=None, password=None):
try:
user = User.objects.get(username=username)
if user.check_password(password):
return user
except User.DoesNotExist:
return None
def get_user(self, user_id):
try:
user = User.objects.get(pk=user_id)
if user.is_active:
return User
return None
except User.DoesNotExist:
return None
請幫助我失去了
三江源我知道這是小東西,它只是沒有露面 – eatingcereal
提示#1:錯誤信息抱怨缺少'self'(第一位置)參數,其通常意味着有人試圖在一個類上調用實例方法。 提示#2:在你的代碼片段中,「User」被突出顯示爲一個讓連接變得明顯的類;) –