0

我最近从我的项目迁移drf-yasgdrf-spectacular,但@extend_schema装饰器似乎没有向我生成的模式添加任何信息:

  • views.py
class SearchView(GenericViewSet):
    serializer_class = serializers.CriterionSerializer
    
    
    @extend_schema(
        methods=['post'],
        summary="Find patients according to search query.",
        description="Search for patients related to Documents found with the given query.",
        request=PolymorphicProxySerializer(
            component_name='Criterion',
            serializers=[
                    serializers.TextCriterionSerializer, serializers.GroupCriterionSerializer,
            ],
            resource_type_field_name='type',
        )
    )
    @action(detail=False, methods=['POST'])
    def fetch(self, request):
        serializer = serializers.CriterionSerializer(data=request.data)
        if not serializer.is_valid(raise_exception=True):
            return Response(serializer.errors, status=400)
        
        return serializers.PatientDocumentSerializer(
            serializer.create(serializer.validated_data).fetch(), context={'request': request},
            many=True
        ).data

这是生成的架构的相关部分:

 /search/fetch/:
    post:
      operationId: searchFetchCreate
      description: ''
      tags:
      - search
      security:
      - jwtAuth: []
      responses:
        '200':
          description: No response body

这里有一些关于 context 的附加信息:

  • urls.py
router = routers.DefaultRouter()
router.register(r'search', views.SearchView, basename='search')

urlpatterns = [
    path('', include(router.urls), name="search"),
]
  • SPECTUACULAR_SETTINGS
SPECTACULAR_SETTINGS = {
    'TITLE': 'API',
    'VERSION': '1.0.0',
    'CAMELIZE_NAMES': True,
    'COMPONENT_SPLIT_REQUEST': True,
    'SERVE_PERMISSIONS': ['rest_framework.permissions.AllowAny'],
    'SWAGGER_UI_SETTINGS': {
        'deepLinking': True,
        'filter': True,
        'displayRequestDuration': True,
        'syntaxHighlight.activate': True,
        'syntaxHighlight.theme': 'monokai',
    },
}
  • 版本:
$ pip3 show django drf-spectacular
Name: Django
Version: 2.2.13
[...]
---
Name: drf-spectacular
Version: 0.17.1
[...]
4

1 回答 1

0

这是由于methods=['post'],而不是methods=['POST'],inside @extend_schema

于 2021-07-26T14:51:42.220 回答