Python Django admin不使用自定义身份验证后端

Python Django admin不使用自定义身份验证后端,python,django,authentication,Python,Django,Authentication,我有一个自定义用户模型和一个身份验证后端。 当我使用shell时,身份验证功能可以很好地工作,但是当涉及到管理员站点登录时,它就是不工作 代码来自 设置.py models.py 因为我在adminauthbend.authenticate()中放置了一个打印,所以我知道何时执行它 当我在shell中调用authenticate()时,在admin页面中调用自定义auth方法,而不是 非常感谢您的帮助。我自己找到了答案 在深入研究Django的源代码之后,我在Django.contrib.aut

我有一个自定义用户模型和一个身份验证后端。 当我使用shell时,身份验证功能可以很好地工作,但是当涉及到管理员站点登录时,它就是不工作

代码来自

设置.py models.py 因为我在adminauthbend.authenticate()中放置了一个
打印
,所以我知道何时执行它

当我在shell中调用
authenticate()
时,在admin页面中调用自定义auth方法,而不是


非常感谢您的帮助。

我自己找到了答案

在深入研究Django的源代码之后,我在
Django.contrib.auth.forms
中找到了这个片段,它处理来自管理站点登录表单的post请求:

def clean(self):
    username = self.cleaned_data.get('username')
    password = self.cleaned_data.get('password')

    if username and password:
        self.user_cache = authenticate(username=username,
                                       password=password)
        if self.user_cache is None:
            raise forms.ValidationError(
                self.error_messages['invalid_login'],
                code='invalid_login',
                params={'username': self.username_field.verbose_name},
            )
        else:
            self.confirm_login_allowed(self.user_cache)

    return self.cleaned_data
请注意,尽管它使用
auth.authentication
来处理登录操作,但它实际上解析固定字段
username
password
,这意味着默认的管理员站点登录表单忽略了您自己的身份验证后端


为了解决这个问题,我需要实现我自己的管理员站点登录表单。

当使用带有电子邮件身份验证的自定义模型时,我遇到了完全相同的问题。我通过创建一个新的后端类来解决这个问题。 据我所知,django正在应用程序中定义的所有后端上循环,检查是否有确切的导入参数

在你的情况下,也许类似的事情会奏效

class EmailAdminBackend(ModelBackend):
def authenticate(self, request, username=None, password=None):
    UserModel = get_user_model()
    try:
        # Check if the user exists in Django's database
        user = UserModel.objects.get(openid=username)
    except :
        return None
    if user.check_password(password):
        return user
    return None

这是我的解决办法。。希望它能帮助某人:

身份验证\u后端=[
“django.contrib.auth.backends.modelbend”#添加此行
'您的授权后端'
]

保留默认类以确保django管理使用的默认用户名/密码身份验证工作流。然后添加自定义身份验证后端类,以便进行更改。

谢谢!我采取了一种不同的方法,通过允许
用户名
密码
定义身份验证(self,request,username=None,password=None,key=None),使
身份验证更加灵活:如果密钥为None:#使用用户名/密码登录管理员通行证#下面是我的原始代码
Hi:),请对您的解决方案提供更多描述。
def clean(self):
    username = self.cleaned_data.get('username')
    password = self.cleaned_data.get('password')

    if username and password:
        self.user_cache = authenticate(username=username,
                                       password=password)
        if self.user_cache is None:
            raise forms.ValidationError(
                self.error_messages['invalid_login'],
                code='invalid_login',
                params={'username': self.username_field.verbose_name},
            )
        else:
            self.confirm_login_allowed(self.user_cache)

    return self.cleaned_data
class EmailAdminBackend(ModelBackend):
def authenticate(self, request, username=None, password=None):
    UserModel = get_user_model()
    try:
        # Check if the user exists in Django's database
        user = UserModel.objects.get(openid=username)
    except :
        return None
    if user.check_password(password):
        return user
    return None