微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

DRF 壮观的没有发现自定义身份验证扩展类 原答案

如何解决DRF 壮观的没有发现自定义身份验证扩展类 原答案

当从 rest_framework_simplejwt.authentication.JWTAuthentication 扩展一个新的令牌认证类时,壮观的 swagger-ui 授权按钮消失并且无法添加令牌承载,我猜当你子类化时它会出错。
重现步骤:
首先,创建一个带有rest框架和drf-spectacular和简单jwt的Django项目,并使用文档指导进行配置。到 /swagger-ui/ 并且它工作正常。
然后创建一个 JWTAuthentication 的子类,如下所示:

from rest_framework_simplejwt.authentication import JWTAuthentication as JWTA

class JWTAuthentication(JWTA):
    pass

并在您的设置中:

REST_FRAMEWORK = {
    'DEFAULT_SCHEMA_CLASS': 'drf_spectacular.openapi.AutoSchema','DEFAULT_AUTHENTICATION_CLASSES': (
        'path_to_your_module.JWTAuthentication',),}

现在,如果您转到 /swagger-ui/,则没有授权按钮!!!我该如何解决这个问题?
我什至尝试创建一个 AuthenticationExtension 像:

from drf_spectacular.contrib.rest_framework_simplejwt import SimpleJWTScheme

class SimpleJWTTokenUserScheme(SimpleJWTScheme):
    target_class = 'path_to_your_module.JWTAuthentication'

但是没有办法在任何地方、互联网或文档中注册它!! 覆盖身份验证类时如何修复授权按钮?
编辑:按照 JPG 所说的做并在设置中导入扩展:

# settings.py
from path.to.custom.extension import SimpleJWTTokenUserScheme
REST_FRAMEWORK = {
    'DEFAULT_SCHEMA_CLASS': 'drf_spectacular.openapi.AutoSchema',}

引发异常:

  File "/home/hamex/current/spec/env/lib/python3.8/site-packages/django/core/handlers/exception.py",line 47,in inner
    response = get_response(request)
  File "/home/hamex/current/spec/env/lib/python3.8/site-packages/django/core/handlers/base.py",line 181,in _get_response
    response = wrapped_callback(request,*callback_args,**callback_kwargs)
  File "/home/hamex/current/spec/env/lib/python3.8/site-packages/django/views/decorators/csrf.py",line 54,in wrapped_view
    return view_func(*args,**kwargs)
  File "/home/hamex/current/spec/env/lib/python3.8/site-packages/django/views/generic/base.py",line 70,in view
    return self.dispatch(request,*args,**kwargs)
  File "/home/hamex/current/spec/env/lib/python3.8/site-packages/rest_framework/views.py",line 509,in dispatch
    response = self.handle_exception(exc)
  File "/home/hamex/current/spec/env/lib/python3.8/site-packages/rest_framework/views.py",line 469,in handle_exception
    self.raise_uncaught_exception(exc)
  File "/home/hamex/current/spec/env/lib/python3.8/site-packages/rest_framework/views.py",line 480,in raise_uncaught_exception
    raise exc
  File "/home/hamex/current/spec/env/lib/python3.8/site-packages/rest_framework/views.py",line 506,in dispatch
    response = handler(request,**kwargs)
  File "/home/hamex/current/spec/env/lib/python3.8/site-packages/drf_spectacular/views.py",line 67,in get
    return self._get_schema_response(request)
  File "/home/hamex/current/spec/env/lib/python3.8/site-packages/drf_spectacular/views.py",line 74,in _get_schema_response
    return Response(generator.get_schema(request=request,public=self.serve_public))
  File "/home/hamex/current/spec/env/lib/python3.8/site-packages/drf_spectacular/generators.py",line 250,in get_schema
    paths=self.parse(request,public),File "/home/hamex/current/spec/env/lib/python3.8/site-packages/drf_spectacular/generators.py",line 218,in parse
    assert isinstance(view.schema,AutoSchema),(
AssertionError: Incompatible AutoSchema used on View <class 'drf_spectacular.views.SpectacularAPIView'>. Is DRF's DEFAULT_SCHEMA_CLASS pointing to "drf_spectacular.openapi.AutoSchema" or any other drf-spectacular compatible AutoSchema?

解决方法

更新-1

来自文档 Where should I put my extensions? / my extensions are not detected

扩展会自动注册自己。只要确定 python 解释器至少会看到它们一次。为此,我们 建议创建一个 PROJECT/schema.py 文件并将其导入您的 PROJECT/__init__.py(与 settings.pyurls.py 相同的目录) 与import PROJECT.schema请不要将文件导入 settings.py 因为这可能会导致循环导入问题。


原答案

这似乎是包本身的错误。在扩展身份验证扩展时,您可以使用实际类而不是类的路径 >

from drf_spectacular.contrib.rest_framework_simplejwt import SimpleJWTScheme
from path.to.custom.jwt.auth import JWTAuthentication

class SimpleJWTTokenUserScheme(SimpleJWTScheme):
    target_class = JWTAuthentication

我在这里创建了一个简单的例子,drf-spectacular-example,希望有人能从中受益!!!

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。