Django REST Framework 在文件之间共享 class 实例

Django REST Framework share class instance between files

我的 apps.py 的 ready() 方法中有一些代码使用 authlib 模块加载一些 Oauth 提供程序。基本上我是这样做的 (apps.py):

from django.apps import AppConfig
from authlib.integrations.django_client import OAuth
from django.core.cache import caches

class ApiConfig(AppConfig):
    name = 'api'

    def ready(self):
        import api.receivers
        defaultcache = caches['default']
        from .modules.oauth import fetch_token
        from .models import OauthProvider, OAuth2Token

        oauth = OAuth(fetch_token=fetch_token)
        print('settings oauthproviders')
        allproviders = OauthProvider.objects.all()
        for provider in allproviders:
            oauth.register(
                name=provider.name,
                client_id=provider.client_id,
                client_secret=provider.client_secret_decrypted,
                access_token_url=provider.access_token_url,
                access_token_params=provider.access_token_params,
                authorize_url=provider.authorize_url,
                authorize_params=provider.authorize_params,
                api_base_url=provider.api_base_url,
                client_kwargs=eval(provider.client_kwargs),
            )

        print(OAuth2Token.objects.get(name='enelogic'))
        defaultcache.set('oauth', oauth)
        #defaultcache.get('oauth') # <--- this line gives error

错误:

Fatal Python error: Cannot recover from stack overflow.
Python runtime state: initialized

Thread 0x00001d4c (most recent call first):
  File "c:\program files (x86)\python38-32\lib\threading.py", line 306 in wait
  File "c:\program files (x86)\python38-32\lib\threading.py", line 558 in wait
  File "c:\Users\ericj\.vscode\extensions\ms-python.python-2020.9.112786\pythonFiles\lib\python\debugpy\_vendored\pydevd\pydevd.py", line 230 in _on_run
  File "c:\Users\ericj\.vscode\extensions\ms-python.python-2020.9.112786\pythonFiles\lib\python\debugpy\_vendored\pydevd\_pydevd_bundle\pydevd_daemon_thread.py", line 46 in run
  File "c:\program files (x86)\python38-32\lib\threading.py", line 932 in _bootstrap_inner
  File "c:\program files (x86)\python38-32\lib\threading.py", line 890 in _bootstrap
  File "c:\Users\ericj\.vscode\extensions\ms-python.python-2020.9.112786\pythonFiles\lib\python\debugpy\_vendored\pydevd\_pydev_bundle\pydev_monkey.py", line 823 in __call__

Thread 0x00002b80 (most recent call first):
  File "c:\program files (x86)\python38-32\lib\threading.py", line 306 in wait
  File "c:\program files (x86)\python38-32\lib\threading.py", line 558 in wait
  File "c:\Users\ericj\.vscode\extensions\ms-python.python-2020.9.112786\pythonFiles\lib\python\debugpy\_vendored\pydevd\pydevd.py", line 184 in _on_run
  File "c:\Users\ericj\.vscode\extensions\ms-python.python-2020.9.112786\pythonFiles\lib\python\debugpy\_vendored\pydevd\_pydevd_bundle\pydevd_daemon_thread.py", line 46 in run
  File "c:\program files (x86)\python38-32\lib\threading.py", line 932 in _bootstrap_inner
  File "c:\program files (x86)\python38-32\lib\threading.py", line 890 in _bootstrap
  File "c:\Users\ericj\.vscode\extensions\ms-python.python-2020.9.112786\pythonFiles\lib\python\debugpy\_vendored\pydevd\_pydev_bundle\pydev_monkey.py", line 823 in __call__

Thread 0x0000609c (most recent call first):
  File "c:\program files (x86)\python38-32\lib\threading.py", line 306 in wait
  File "c:\program files (x86)\python38-32\lib\threading.py", line 558 in wait
  File "c:\Users\ericj\.vscode\extensions\ms-python.python-2020.9.112786\pythonFiles\lib\python\debugpy\_vendored\pydevd\_pydevd_bundle\pydevd_timeout.py", line 43 in _on_run
  File "c:\Users\ericj\.vscode\extensions\ms-python.python-2020.9.112786\pythonFiles\lib\python\debugpy\_vendored\pydevd\_pydevd_bundle\pydevd_daemon_thread.py", line 46 in run
  File "c:\program files (x86)\python38-32\lib\threading.py", line 932 in _bootstrap_inner
  File "c:\program files (x86)\python38-32\lib\threading.py", line 890 in _bootstrap

Thread 0x00000ad4 (most recent call first):
  File "c:\Users\ericj\.vscode\extensions\ms-python.python-2020.9.112786\pythonFiles\lib\python\debugpy\_vendored\pydevd\_pydevd_bundle\pydevd_comm.py", line 209 in _read_line
  File "c:\Users\ericj\.vscode\extensions\ms-python.python-2020.9.112786\pythonFiles\lib\python\debugpy\_vendored\pydevd\_pydevd_bundle\pydevd_comm.py", line 227 in _on_run
  File "c:\Users\ericj\.vscode\extensions\ms-python.python-2020.9.112786\pythonFiles\lib\python\debugpy\_vendored\pydevd\_pydevd_bundle\pydevd_daemon_thread.py", line 46 in run
  File "c:\program files (x86)\python38-32\lib\threading.py", line 932 in _bootstrap_inner
  File "c:\program files (x86)\python38-32\lib\threading.py", line 890 in _bootstrap

Thread 0x00005cf8 (most recent call first):
  File "c:\Users\ericj\.vscode\extensions\ms-python.python-2020.9.112786\pythonFiles\lib\python\debugpy\_vendored\pydevd\_pydevd_bundle\pydevd_net_command.py", line 111 in send
  File "c:\Users\ericj\.vscode\extensions\ms-python.python-2020.9.112786\pythonFiles\lib\python\debugpy\_vendored\pydevd\_pydevd_bundle\pydevd_comm.py", line 370 in _on_run
  File "c:\Users\ericj\.vscode\extensions\ms-python.python-2020.9.112786\pythonFiles\lib\python\debugpy\_vendored\pydevd\_pydevd_bundle\pydevd_daemon_thread.py", line 46 in run
  File "c:\program files (x86)\python38-32\lib\threading.py", line 932 in _bootstrap_inner
  File "c:\program files (x86)\python38-32\lib\threading.py", line 890 in _bootstrap

Current thread 0x00005924 (most recent call first):
  File "c:\program files (x86)\python38-32\lib\traceback.py", line 476 in __init__
  File "c:\program files (x86)\python38-32\lib\traceback.py", line 493 in __init__
  File "c:\program files (x86)\python38-32\lib\traceback.py", line 493 in __init__
  File "c:\program files (x86)\python38-32\lib\traceback.py", line 493 in __init__
  File "c:\program files (x86)\python38-32\lib\traceback.py", line 493 in __init__
  File "c:\program files (x86)\python38-32\lib\traceback.py", line 493 in __init__
  File "c:\program files (x86)\python38-32\lib\traceback.py", line 493 in __init__
  File "c:\program files (x86)\python38-32\lib\traceback.py", line 493 in __init__
  File "c:\program files (x86)\python38-32\lib\traceback.py", line 493 in __init__
  File "c:\program files (x86)\python38-32\lib\traceback.py", line 493 in __init__
  File "c:\program files (x86)\python38-32\lib\traceback.py", line 493 in __init__
  File "c:\program files (x86)\python38-32\lib\traceback.py", line 493 in __init__
  File "c:\program files (x86)\python38-32\lib\traceback.py", line 493 in __init__
  File "c:\program files (x86)\python38-32\lib\traceback.py", line 493 in __init__
  File "c:\program files (x86)\python38-32\lib\traceback.py", line 493 in __init__
  File "c:\program files (x86)\python38-32\lib\traceback.py", line 493 in __init__
  File "c:\program files (x86)\python38-32\lib\traceback.py", line 493 in __init__
  File "c:\program files (x86)\python38-32\lib\traceback.py", line 493 in __init__
  File "c:\program files (x86)\python38-32\lib\traceback.py", line 493 in __init__
  File "c:\program files (x86)\python38-32\lib\traceback.py", line 493 in __init__
  File "c:\program files (x86)\python38-32\lib\traceback.py", line 493 in __init__
  File "c:\program files (x86)\python38-32\lib\traceback.py", line 493 in __init__
  File "c:\program files (x86)\python38-32\lib\traceback.py", line 493 in __init__
  File "c:\program files (x86)\python38-32\lib\traceback.py", line 103 in print_exception
  File "c:\program files (x86)\python38-32\lib\traceback.py", line 163 in print_exc
  File "c:\Users\ericj\.vscode\extensions\ms-python.python-2020.9.112786\pythonFiles\lib\python\debugpy\_vendored\pydevd\_pydev_bundle\pydev_log.py", line 130 in _pydevd_log_exception
  File "c:\Users\ericj\.vscode\extensions\ms-python.python-2020.9.112786\pythonFiles\lib\python\debugpy\_vendored\pydevd\_pydev_bundle\pydev_log.py", line 160 in exception
  File "c:\Users\ericj\.vscode\extensions\ms-python.python-2020.9.112786\pythonFiles\lib\python\debugpy\_vendored\pydevd\_pydevd_bundle\pydevd_trace_dispatch_regular.py", line 462 in __call__
  File "C:\Users\ericj\.virtualenvs\django_appelent_api-8qkCZVAS\lib\site-packages\authlib\integrations\base_client\base_oauth.py", line 101 in __getattr__
  File "C:\Users\ericj\.virtualenvs\django_appelent_api-8qkCZVAS\lib\site-packages\authlib\integrations\base_client\base_oauth.py", line 105 in __getattr__
  File "C:\Users\ericj\.virtualenvs\django_appelent_api-8qkCZVAS\lib\site-packages\authlib\integrations\base_client\base_oauth.py", line 105 in __getattr__
  File "C:\Users\ericj\.virtualenvs\django_appelent_api-8qkCZVAS\lib\site-packages\authlib\integrations\base_client\base_oauth.py", line 105 in __getattr__
  File "C:\Users\ericj\.virtualenvs\django_appelent_api-8qkCZVAS\lib\site-packages\authlib\integrations\base_client\base_oauth.py", line 105 in __getattr__
  File "C:\Users\ericj\.virtualenvs\django_appelent_api-8qkCZVAS\lib\site-packages\authlib\integrations\base_client\base_oauth.py", line 105 in __getattr__
  File "C:\Users\ericj\.virtualenvs\django_appelent_api-8qkCZVAS\lib\site-packages\authlib\integrations\base_client\base_oauth.py", line 105 in __getattr__
  File "C:\Users\ericj\.virtualenvs\django_appelent_api-8qkCZVAS\lib\site-packages\authlib\integrations\base_client\base_oauth.py", line 105 in __getattr__
  File "C:\Users\ericj\.virtualenvs\django_appelent_api-8qkCZVAS\lib\site-packages\authlib\integrations\base_client\base_oauth.py", line 105 in __getattr__
  File "C:\Users\ericj\.virtualenvs\django_appelent_api-8qkCZVAS\lib\site-packages\authlib\integrations\base_client\base_oauth.py", line 105 in __getattr__
  File "C:\Users\ericj\.virtualenvs\django_appelent_api-8qkCZVAS\lib\site-packages\authlib\integrations\base_client\base_oauth.py", line 105 in __getattr__
  File "C:\Users\ericj\.virtualenvs\django_appelent_api-8qkCZVAS\lib\site-packages\authlib\integrations\base_client\base_oauth.py", line 105 in __getattr__
  File "C:\Users\ericj\.virtualenvs\django_appelent_api-8qkCZVAS\lib\site-packages\authlib\integrations\base_client\base_oauth.py", line 105 in __getattr__
  File "C:\Users\ericj\.virtualenvs\django_appelent_api-8qkCZVAS\lib\site-packages\authlib\integrations\base_client\base_oauth.py", line 105 in __getattr__
  File "C:\Users\ericj\.virtualenvs\django_appelent_api-8qkCZVAS\lib\site-packages\authlib\integrations\base_client\base_oauth.py", line 105 in __getattr__
  File "C:\Users\ericj\.virtualenvs\django_appelent_api-8qkCZVAS\lib\site-packages\authlib\integrations\base_client\base_oauth.py", line 105 in __getattr__
  File "C:\Users\ericj\.virtualenvs\django_appelent_api-8qkCZVAS\lib\site-packages\authlib\integrations\base_client\base_oauth.py", line 105 in __getattr__
  File "C:\Users\ericj\.virtualenvs\django_appelent_api-8qkCZVAS\lib\site-packages\authlib\integrations\base_client\base_oauth.py", line 105 in __getattr__
  File "C:\Users\ericj\.virtualenvs\django_appelent_api-8qkCZVAS\lib\site-packages\authlib\integrations\base_client\base_oauth.py", line 105 in __getattr__
  File "C:\Users\ericj\.virtualenvs\django_appelent_api-8qkCZVAS\lib\site-packages\authlib\integrations\base_client\base_oauth.py", line 105 in __getattr__
  File "C:\Users\ericj\.virtualenvs\django_appelent_api-8qkCZVAS\lib\site-packages\authlib\integrations\base_client\base_oauth.py", line 105 in __getattr__
  File "C:\Users\ericj\.virtualenvs\django_appelent_api-8qkCZVAS\lib\site-packages\authlib\integrations\base_client\base_oauth.py", line 105 in __getattr__
  File "C:\Users\ericj\.virtualenvs\django_appelent_api-8qkCZVAS\lib\site-packages\authlib\integrations\base_client\base_oauth.py", line 105 in __getattr__
  File "C:\Users\ericj\.virtualenvs\django_appelent_api-8qkCZVAS\lib\site-packages\authlib\integrations\base_client\base_oauth.py", line 105 in __getattr__
  File "C:\Users\ericj\.virtualenvs\django_appelent_api-8qkCZVAS\lib\site-packages\authlib\integrations\base_client\base_oauth.py", line 105 in __getattr__
  File "C:\Users\ericj\.virtualenvs\django_appelent_api-8qkCZVAS\lib\site-packages\authlib\integrations\base_client\base_oauth.py", line 105 in __getattr__
  File "C:\Users\ericj\.virtualenvs\django_appelent_api-8qkCZVAS\lib\site-packages\authlib\integrations\base_client\base_oauth.py", line 105 in __getattr__
  File "C:\Users\ericj\.virtualenvs\django_appelent_api-8qkCZVAS\lib\site-packages\authlib\integrations\base_client\base_oauth.py", line 105 in __getattr__
  File "C:\Users\ericj\.virtualenvs\django_appelent_api-8qkCZVAS\lib\site-packages\authlib\integrations\base_client\base_oauth.py", line 105 in __getattr__
  File "C:\Users\ericj\.virtualenvs\django_appelent_api-8qkCZVAS\lib\site-packages\authlib\integrations\base_client\base_oauth.py", line 105 in __getattr__
  File "C:\Users\ericj\.virtualenvs\django_appelent_api-8qkCZVAS\lib\site-packages\authlib\integrations\base_client\base_oauth.py", line 105 in __getattr__
  File "C:\Users\ericj\.virtualenvs\django_appelent_api-8qkCZVAS\lib\site-packages\authlib\integrations\base_client\base_oauth.py", line 105 in __getattr__
  File "C:\Users\ericj\.virtualenvs\django_appelent_api-8qkCZVAS\lib\site-packages\authlib\integrations\base_client\base_oauth.py", line 105 in __getattr__
  File "C:\Users\ericj\.virtualenvs\django_appelent_api-8qkCZVAS\lib\site-packages\authlib\integrations\base_client\base_oauth.py", line 105 in __getattr__
  File "C:\Users\ericj\.virtualenvs\django_appelent_api-8qkCZVAS\lib\site-packages\authlib\integrations\base_client\base_oauth.py", line 105 in __getattr__
  File "C:\Users\ericj\.virtualenvs\django_appelent_api-8qkCZVAS\lib\site-packages\authlib\integrations\base_client\base_oauth.py", line 105 in __getattr__
  File "C:\Users\ericj\.virtualenvs\django_appelent_api-8qkCZVAS\lib\site-packages\authlib\integrations\base_client\base_oauth.py", line 105 in __getattr__
  File "C:\Users\ericj\.virtualenvs\django_appelent_api-8qkCZVAS\lib\site-packages\authlib\integrations\base_client\base_oauth.py", line 105 in __getattr__
  File "C:\Users\ericj\.virtualenvs\django_appelent_api-8qkCZVAS\lib\site-packages\authlib\integrations\base_client\base_oauth.py", line 105 in __getattr__
  File "C:\Users\ericj\.virtualenvs\django_appelent_api-8qkCZVAS\lib\site-packages\authlib\integrations\base_client\base_oauth.py", line 105 in __getattr__
  File "C:\Users\ericj\.virtualenvs\django_appelent_api-8qkCZVAS\lib\site-packages\authlib\integrations\base_client\base_oauth.py", line 105 in __getattr__
  File "C:\Users\ericj\.virtualenvs\django_appelent_api-8qkCZVAS\lib\site-packages\authlib\integrations\base_client\base_oauth.py", line 105 in __getattr__
  File "C:\Users\ericj\.virtualenvs\django_appelent_api-8qkCZVAS\lib\site-packages\authlib\integrations\base_client\base_oauth.py", line 105 in __getattr__
  File "C:\Users\ericj\.virtualenvs\django_appelent_api-8qkCZVAS\lib\site-packages\authlib\integrations\base_client\base_oauth.py", line 105 in __getattr__
  File "C:\Users\ericj\.virtualenvs\django_appelent_api-8qkCZVAS\lib\site-packages\authlib\integrations\base_client\base_oauth.py", line 105 in __getattr__
  File "C:\Users\ericj\.virtualenvs\django_appelent_api-8qkCZVAS\lib\site-packages\authlib\integrations\base_client\base_oauth.py", line 105 in __getattr__
  File "C:\Users\ericj\.virtualenvs\django_appelent_api-8qkCZVAS\lib\site-packages\authlib\integrations\base_client\base_oauth.py", line 105 in __getattr__
  File "C:\Users\ericj\.virtualenvs\django_appelent_api-8qkCZVAS\lib\site-packages\authlib\integrations\base_client\base_oauth.py", line 105 in __getattr__
  File "C:\Users\ericj\.virtualenvs\django_appelent_api-8qkCZVAS\lib\site-packages\authlib\integrations\base_client\base_oauth.py", line 105 in __getattr__
  File "C:\Users\ericj\.virtualenvs\django_appelent_api-8qkCZVAS\lib\site-packages\authlib\integrations\base_client\base_oauth.py", line 105 in __getattr__
  File "C:\Users\ericj\.virtualenvs\django_appelent_api-8qkCZVAS\lib\site-packages\authlib\integrations\base_client\base_oauth.py", line 105 in __getattr__
  File "C:\Users\ericj\.virtualenvs\django_appelent_api-8qkCZVAS\lib\site-packages\authlib\integrations\base_client\base_oauth.py", line 105 in __getattr__
  File "C:\Users\ericj\.virtualenvs\django_appelent_api-8qkCZVAS\lib\site-packages\authlib\integrations\base_client\base_oauth.py", line 105 in __getattr__
  File "C:\Users\ericj\.virtualenvs\django_appelent_api-8qkCZVAS\lib\site-packages\authlib\integrations\base_client\base_oauth.py", line 105 in __getattr__
  File "C:\Users\ericj\.virtualenvs\django_appelent_api-8qkCZVAS\lib\site-packages\authlib\integrations\base_client\base_oauth.py", line 105 in __getattr__
  File "C:\Users\ericj\.virtualenvs\django_appelent_api-8qkCZVAS\lib\site-packages\authlib\integrations\base_client\base_oauth.py", line 105 in __getattr__
  File "C:\Users\ericj\.virtualenvs\django_appelent_api-8qkCZVAS\lib\site-packages\authlib\integrations\base_client\base_oauth.py", line 105 in __getattr__
  File "C:\Users\ericj\.virtualenvs\django_appelent_api-8qkCZVAS\lib\site-packages\authlib\integrations\base_client\base_oauth.py", line 105 in __getattr__
  File "C:\Users\ericj\.virtualenvs\django_appelent_api-8qkCZVAS\lib\site-packages\authlib\integrations\base_client\base_oauth.py", line 105 in __getattr__
  File "C:\Users\ericj\.virtualenvs\django_appelent_api-8qkCZVAS\lib\site-packages\authlib\integrations\base_client\base_oauth.py", line 105 in __getattr__
  File "C:\Users\ericj\.virtualenvs\django_appelent_api-8qkCZVAS\lib\site-packages\authlib\integrations\base_client\base_oauth.py", line 105 in __getattr__
  File "C:\Users\ericj\.virtualenvs\django_appelent_api-8qkCZVAS\lib\site-packages\authlib\integrations\base_client\base_oauth.py", line 105 in __getattr__
  File "C:\Users\ericj\.virtualenvs\django_appelent_api-8qkCZVAS\lib\site-packages\authlib\integrations\base_client\base_oauth.py", line 105 in __getattr__
  File "C:\Users\ericj\.virtualenvs\django_appelent_api-8qkCZVAS\lib\site-packages\authlib\integrations\base_client\base_oauth.py", line 105 in __getattr__
  File "C:\Users\ericj\.virtualenvs\django_appelent_api-8qkCZVAS\lib\site-packages\authlib\integrations\base_client\base_oauth.py", line 105 in __getattr__
  File "C:\Users\ericj\.virtualenvs\django_appelent_api-8qkCZVAS\lib\site-packages\authlib\integrations\base_client\base_oauth.py", line 105 in __getattr__
  File "C:\Users\ericj\.virtualenvs\django_appelent_api-8qkCZVAS\lib\site-packages\authlib\integrations\base_client\base_oauth.py", line 105 in __getattr__
  File "C:\Users\ericj\.virtualenvs\django_appelent_api-8qkCZVAS\lib\site-packages\authlib\integrations\base_client\base_oauth.py", line 105 in __getattr__
  File "C:\Users\ericj\.virtualenvs\django_appelent_api-8qkCZVAS\lib\site-packages\authlib\integrations\base_client\base_oauth.py", line 105 in __getattr__
  File "C:\Users\ericj\.virtualenvs\django_appelent_api-8qkCZVAS\lib\site-packages\authlib\integrations\base_client\base_oauth.py", line 105 in __getattr__
  File "C:\Users\ericj\.virtualenvs\django_appelent_api-8qkCZVAS\lib\site-packages\authlib\integrations\base_client\base_oauth.py", line 105 in __getattr__
  File "C:\Users\ericj\.virtualenvs\django_appelent_api-8qkCZVAS\lib\site-packages\authlib\integrations\base_client\base_oauth.py", line 105 in __getattr__

这会注册一些可以使用 oauth 变量使用的 oauth 凭据(信息)(例如 oauth.get_authorization_url() 这让我获得了该 Oauth 提供商的授权 URL。

现在,我有很多不同的文件(我将 views.py 分成多个文件)。我如何访问这些文件中的这个 oauth 变量?我试过使用 django 缓存,但是当我尝试访问变量时出现奇怪的溢出错误。

你会如何推荐?

oauth class 在本地文件中工作正常,我只需要将它传输到其他文件

为什么不将其提取到单独的 python 文件中(但仍在您的 Django 应用程序中)。

# oauth_providers.py

from authlib.integrations.django_client import OAuth

from .modules.oauth import fetch_token
from .models import OauthProvider, OAuth2Token


def register_providers(oauth):
    print('settings oauthproviders')
    allproviders = OauthProvider.objects.all()
    for provider in allproviders:
        oauth.register(
            name=provider.name,
            client_id=provider.client_id,
            client_secret=provider.client_secret_decrypted,
            access_token_url=provider.access_token_url,
            access_token_params=provider.access_token_params,
            authorize_url=provider.authorize_url,
            authorize_params=provider.authorize_params,
            api_base_url=provider.api_base_url,
            client_kwargs=eval(provider.client_kwargs),
        )

    print(OAuth2Token.objects.get(name='enelogic'))

oauth = OAuth(fetch_token=fetch_token)
# apps.py
from django.apps import AppConfig


class ApiConfig(AppConfig):
    name = 'api'

    def ready(self):
        from .oauth_providers import oauth, register_providers
        register_providers(oauth)

现在您可以通过简单的导入在您的视图中使用它

# views.py

from .oauth_providers import oauth

# do whatever with oauth

是的,有一些注意事项,它将是一个全局变量(所以不要尝试更改,我认为您不需要)并且不要尝试将文件导入您的 models.py 因为这应该在应用 ready().

之后导入