如何重写Django中的package方法?

2024-04-29 15:11:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图重写通过pip安装的父类方法。但是,问题是我不确定如何调用重写的类,以便它使用我的方法而不是父类方法。这是我试图重写的方法:

class OIDCAuthenticationBackend(ModelBackend):
def verify_claims(self, claims):
        """Verify the provided claims to decide if authentication should be allowed."""

        # Verify claims required by default configuration
        scopes = self.get_settings('OIDC_RP_SCOPES', 'openid email')
        if 'email' in scopes.split():
            return 'email' in claims

        LOGGER.warning('Custom OIDC_RP_SCOPES defined. '
                       'You need to override `verify_claims` for custom claims verification.')

        return True

我重写的方法是:

模型.py

^{pr2}$

我已经写在上面了模型.py根据一些文件,我读了重写,但我不确定我应该在哪里写这篇文章。在

然后尝试从我的视图调用它,如下所示:

视图.py

^{3}$

但后来我得到了一个错误:

from myapp import MyOIDCAB

ImportError: cannot import name 'MyOIDCAB'

我不确定我是说对了还是错了? 我的项目结构是:

myproject
myapp
templates
manage.py

在包站点,他们在模板中这样调用它:

 <a href="{% url 'oidc_authentication_init'%}">Login</a> 

Tags: to方法pyselfauthenticationifemailrp