首页 > 解决方案 > 如何覆盖 Django 中的包方法?

问题描述

我正在尝试覆盖我通过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


from mozilla_django_oidc.auth import OIDCAuthenticationBackend

class MyOIDCAB(OIDCAuthenticationBackend):
    def verify_claims(self, claims):
         return True

根据我阅读的一些文档,我已经在models.py上写了这个,但我不确定我应该在哪里写这个。

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

视图.py

from myapp import MyOIDCAB

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

from myapp import MyOIDCAB

ImportError:无法导入名称“MyOIDCAB”

我不确定我以正确或错误的方式称呼它?我的项目结构是:

myproject
myapp
templates
manage.py

从包站点,他们在模板中调用它,如下所示:

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

标签: pythondjangooverriding

解决方案


推荐阅读