1

我是第一次集成drf-yasq swagger,对不起!如果我的问题很愚蠢。我从这里阅读了文档https://drf-yasg.readthedocs.io/en/stable/readme.html并按照说明操作并以AttributeError: 'Response' object has no attribute 'label'. 原始代码来自https://drf-yasg.readthedocs.io/en/stable/readme.html。请在我缺乏的地方帮助我:

终端:

 Internal Server Error: /swagger/
 Traceback (most recent call last):
 File "/Users/admin/Desktop/food-delivery-app-19083590-python/env/lib/python3.7/site- 
 packages/django/core/handlers/exception.py", line 34, in inner
  response = get_response(request)
 File "/Users/admin/Desktop/food-delivery-app-19083590-python/env/lib/python3.7/site- 
  packages/django/core/handlers/base.py", line 115, in _get_response
  response = self.process_exception_by_middleware(e, request)
 File "/Users/admin/Desktop/food-delivery-app-19083590-python/env/lib/python3.7/site- 
   packages/django/core/handlers/base.py", line 113, in _get_response
   response = wrapped_callback(request, *callback_args, **callback_kwargs)
 File "/Users/admin/Desktop/food-delivery-app-19083590-python/env/lib/python3.7/site- 
  packages/django/views/decorators/csrf.py", line 54, in wrapped_view
  r . eturn view_func(*args, **kwargs)
 File "/Users/admin/Desktop/food-delivery-app-19083590-python/env/lib/python3.7/site- 
   packages/django/views/generic/base.py", line 71, in view
   return self.dispatch(request, *args, **kwargs)
 File "/Users/admin/Desktop/food-delivery-app-19083590-python/env/lib/python3.7/site- 
  packages/rest_framework/views.py", line 497, in dispatch
  response = self.handle_exception(exc)
 File "/Users/admin/Desktop/food-delivery-app-19083590-python/env/lib/python3.7/site- 
   packages/rest_framework/views.py", line 457, in handle_exception
   self.raise_uncaught_exception(exc)
 File "/Users/admin/Desktop/food-delivery-app-19083590-python/env/lib/python3.7/site- 
   packages/rest_framework/views.py", line 468, in raise_uncaught_exception
   raise exc
 File "/Users/admin/Desktop/food-delivery-app-19083590-python/env/lib/python3.7/site- 
   packages/rest_framework/views.py", line 494, in dispatch
   response = handler(request, *args, **kwargs)
 File "/Users/admin/Desktop/food-delivery-app-19083590-python/env/lib/python3.7/site- 
   packages/drf_yasg/views.py", line 94, in get
   schema = generator.get_schema(request, self.public)
 File "/Users/admin/Desktop/food-delivery-app-19083590-python/env/lib/python3.7/site- 
   packages/drf_yasg/generators.py", line 254, in get_schema
   paths, prefix = self.get_paths(endpoints, components, request, public)
 File "/Users/admin/Desktop/food-delivery-app-19083590-python/env/lib/python3.7/site- 
   packages/drf_yasg/generators.py", line 412, in get_paths
   operation = self.get_operation(view, path, prefix, method, components, request)
 File "/Users/admin/Desktop/food-delivery-app-19083590-python/env/lib/python3.7/site- 
   packages/drf_yasg/generators.py", line 454, in get_operation
   operation = view_inspector.get_operation(operation_keys)
 File "/Users/admin/Desktop/food-delivery-app-19083590-python/env/lib/python3.7/site- 
   packages/drf_yasg/inspectors/view.py", line 45, in get_operation
   responses = self.get_responses()
 File "/Users/admin/Desktop/food-delivery-app-19083590-python/env/lib/python3.7/site- 
   packages/drf_yasg/inspectors/view.py", line 180, in get_responses
   response_serializers = self.get_response_serializers()
 File "/Users/admin/Desktop/food-delivery-app-19083590-python/env/lib/python3.7/site- 
   packages/drf_yasg/inspectors/view.py", line 235, in get_response_serializers
   responses = self.get_default_responses()
 File "/Users/admin/Desktop/food-delivery-app-19083590-python/env/lib/python3.7/site- 
   packages/drf_yasg/inspectors/view.py", line 211, in get_default_responses
   default_schema = self.serializer_to_schema(default_schema) or ''
 File "/Users/admin/Desktop/food-delivery-app-19083590-python/env/lib/python3.7/site- 
   packages/drf_yasg/inspectors/base.py", line 446, in serializer_to_schema
   self.field_inspectors, 'get_schema', serializer, {'field_inspectors': 
   self.field_inspectors}
 File "/Users/admin/Desktop/food-delivery-app-19083590-python/env/lib/python3.7/site- 
   packages/drf_yasg/inspectors/base.py", line 118, in probe_inspectors
   result = method(obj, **kwargs)
 File "/Users/admin/Desktop/food-delivery-app-19083590-python/env/lib/python3.7/site- 
    packages/drf_yasg/inspectors/field.py", line 42, in get_schema
    return self.probe_field_inspectors(serializer, openapi.Schema, self.use_definitions)
 File "/Users/admin/Desktop/food-delivery-app-19083590-python/env/lib/python3.7/site- 
    packages/drf_yasg/inspectors/base.py", line 238, in probe_field_inspectors
    swagger_object_type=swagger_object_type, use_references=use_references, **kwargs
 File "/Users/admin/Desktop/food-delivery-app-19083590-python/env/lib/python3.7/site- 
     packages/drf_yasg/inspectors/base.py", line 118, in probe_inspectors
     result = method(obj, **kwargs)
 File "/Users/admin/Desktop/food-delivery-app-19083590-python/env/lib/python3.7/site- 
     packages/drf_yasg/inspectors/field.py", line 83, in field_to_swagger_object
     SwaggerType, ChildSwaggerType = self._get_partial_types(field, swagger_object_type, 
     use_references, **kwargs)
 File "/Users/admin/Desktop/food-delivery-app-19083590-python/env/lib/python3.7/site- 
     packages/drf_yasg/inspectors/base.py", line 273, in _get_partial_types
     title = force_real_str(field.label) if field.label else None
 AttributeError: 'Response' object has no attribute 'label'
 HTTP GET /swagger/?format=openapi 500 [2.48, 127.0.0.1:51305]

设置.py

INSTALLED_APPS=[
         ...
         'drf_yasg',
         ...
]

SWAGGER_SETTINGS = {
   'SECURITY_DEFINITIONS': {
        'basic': {
           'type': 'basic'
        }
    },
 }

网址.py

from drf_yasg.views import get_schema_view
from drf_yasg import openapi

schema_view = get_schema_view(
   openapi.Info(
      title="Food Delivery Application",
      default_version='0.1',
      description="Test description",
   ),
       public=True,
       permission_classes=(permissions.AllowAny,),
)


urlpatterns = [
    ...
    url(r'^swagger(?P<format>\.json|\.yaml)$', schema_view.without_ui(cache_timeout=0), name='schema-json'),
    url(r'^swagger/$', schema_view.with_ui('swagger', cache_timeout=0), name='schema-swagger-ui'),
    ...
]
4

1 回答 1

3

在我的代码上花了几个小时后,我终于让它工作了。 在使用以下编码技术来覆盖和过滤经过身份验证的用户的数据来解决问题时,drf-yasg将无法正常工作:get_queryset()get_serializer()self.request.user

def get_queryset(self):
    if getattr(self, 'swagger_fake_view', False):
        return MyModel.objects.none()  # return empty queryset
    return self.queryset.filter(user=self.request.user)

要了解更多请点击这里

于 2019-12-23T06:35:42.430 回答