我们如何在django rest swagger UI中使用API​​VIEW为django-rest api指定标头参数和后置参数

时间:2019-03-15 09:51:08

标签: django django-rest-framework django-rest-swagger

这是我的app / views.py

class ExampleView(APIView):

    def get(self, request, format=None):
        print(request.META['HTTP_HEADER'])  # Want this header from swagger ui
        snippets = Snippet.objects.all()
        serializer = SnippetSerializer(snippets, many=True)
        return Response(serializer.data)

    def post(self, request, format=None):
        serializer = SnippetSerializer(data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

app / serilaizers.py

class SnippetSerializer(serializers.ModelSerializer):

    class Meta:
        model = Snippet
        fields = ('id', 'title', 'code', 'linenos', 'language', 'style', 'owner')

swagger UI的当前屏幕截图,因为没有用于标题参数和后置参数的字段。

版本:

django-rest-swagger == 2.1.0

djangorestframework == 3.9.1

0 个答案:

没有答案