Python 如何在Django中重写包方法?
我试图重写通过pip安装的父类方法。但是,问题是我不确定如何调用我的重写类,以便它使用我的方法而不是父类方法。这是我试图覆盖的方法:Python 如何在Django中重写包方法?,python,django,overriding,Python,Django,Overriding,我试图重写通过pip安装的父类方法。但是,问题是我不确定如何调用我的重写类,以便它使用我的方法而不是父类方法。这是我试图覆盖的方法: class OIDCAuthenticationBackend(ModelBackend): def verify_claims(self, claims): """Verify the provided claims to decide if authentication should be allowed.""" # Veri
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
from myapp import MyOIDCAB
我已经在models.py上写了这篇文章,这是根据我读过的一些文档编写的,但是我不确定应该在哪里写这篇文章
然后尝试从我的视图调用它,函数如下:
<a href="{% url 'oidc_authentication_init'%}">Login</a>
视图.py
from mozilla_django_oidc.auth import OIDCAuthenticationBackend
class MyOIDCAB(OIDCAuthenticationBackend):
def verify_claims(self, claims):
return True
from myapp import MyOIDCAB
但后来我犯了一个错误:
ImportError:无法导入名称“MyOIDCAB”
我不确定我说的是对的还是错的?
我的项目结构是:
myproject
myapp
templates
manage.py
从包站点,他们在模板中调用它,如下所示:
<a href="{% url 'oidc_authentication_init'%}">Login</a>
覆盖没有问题,问题在于导入,请查看在声明MyOIDCAB
类的文件夹的根目录中是否有\uuu init\uuuuuuuuuuy.py
。您需要\uuuu init\uuuu.py
文件才能导入模块。我的两个\uuuu init\uuuuuu.py
文件是空的@JosipKolarić它的名称是init.py
还是\uu init\uuuuuu.py
?它的\uuuu init\uuuu.py
如果为空,则不能从目录导入MyClass执行,因为目录本身没有该类-需要从目录、模块导入MyClass执行<代码>从myapp.models导入MyOIDCAB