python - 如何覆盖 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>
解决方案
推荐阅读
- sql - 如何使用 SQL (postgresql) 查询有条件地更改每个组内的值?
- angular - 角度数据表:如何从数组中读取数据?
- html - 将 div 定位到容器的底部
- linux - Linux iostat 在一段时间内的“平均”结果
- apache - 我已经更新了我的 apache 版本,但 curl 仍然显示早期版本
- java - 不确定为什么这个游戏对象不会显示在屏幕上
- elasticsearch - Serilog-logstash 索引
- sql - 如何使用orderby始终首先检索记录和其余记录?
- regex - 正则表达式单引号和双引号不起作用
- xamarin.forms - 绑定到自定义控件中的属性