У меня есть пользовательский «AUTH_USER_MODEL» следующим образом:
class User(AbstractUser):
is_institute_admin = models.BooleanField(default=False)
def has_perm(self, perm, obj=None):
app_label = perm.split('.')[0]
if self.is_institute_admin:
return app_label == 'Profiler'
if self.is_superuser:
return app_label == 'Accountant'
def has_module_perms(self, app_label):
if self.is_institute_admin:
return app_label == 'Profiler'
if self.is_superuser:
return app_label == 'Accountant'
Я хочу, чтобы при входе пользователя в административную панель django аутентификация должна была проверять is_institute_admin
вместо ее по умолчанию is_staff
.
Я добавил и зарегистрировал в settings.py
пользовательский файл аутентификации, как показано ниже:
class CustomBackend:
def authenticate(self, request, username=None, password=None):
try:
user = User.objects.get(username=username)
print('user value =', user, password)
password_valid = check_password(password=password, encoded=user.password)
if password_valid:
if user.is_superuser or user.is_institute_admin:
print('returning user')
return user
return None
else:
print('password not matched')
return None
except User.DoesNotExist:
return None
def get_user(self, user_id):
try:
return User.objects.get(pk=user_id)
except User.DoesNotExist:
return None
Что касается отладки, я добавил несколько операторов печати, и она печатается до print('returning user')
, но аутентификация не удалась.
Заранее спасибо за помощь!