AttributeError: 'Response' object has no attribute 'label' drf-yasg
AttributeError: 'Response' object has no attribute 'label' drf-yasg
第一次集成drf-yasq swagger,抱歉!如果我的问题很愚蠢。我从这里 https://drf-yasg.readthedocs.io/en/stable/readme.html and followed the instructions and end up with AttributeError: 'Response' object has no attribute 'label'
. original code is from 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'),
...
]
在我的代码上花了几个小时之后,我终于让它工作了。 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
第一次集成drf-yasq swagger,抱歉!如果我的问题很愚蠢。我从这里 https://drf-yasg.readthedocs.io/en/stable/readme.html and followed the instructions and end up with AttributeError: 'Response' object has no attribute 'label'
. original code is from 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'),
...
]
在我的代码上花了几个小时之后,我终于让它工作了。 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