簡體   English   中英

如何覆蓋 Django 中的 package 方法?

[英]How to override a package method in Django?

我正在嘗試覆蓋我通過pip安裝的父 class 方法。 但是,問題是我不確定如何調用我的重寫 class 以便它將使用我的方法而不是父 class 方法。 這是我試圖覆蓋的方法:

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

導入錯誤:無法導入名稱“MyOIDCAB”

我不確定我是以正確的方式調用它還是錯誤的? 我的項目結構是:

myproject
myapp
templates
manage.py

在 package 網站上,他們在模板中這樣稱呼它:

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

class 在 models.py 文件中,所以你應該如何導入它:

from myapp.models import MyOIDCAB

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM