抱歉,我是第一次集成drf-yasq摇摇杆!如果我的问题很愚蠢。我从这里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]
setting.py
INSTALLED_APPS=[
...
'drf_yasg',
...
]
SWAGGER_SETTINGS = {
'SECURITY_DEFINITIONS': {
'basic': {
'type': 'basic'
}
},
}
urls.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'),
...
]
答案 0 :(得分:1)
花了我几个小时的代码,我终于使它工作了。 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)
了解更多click here