4

这是我的应用程序/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)

应用程序/serilaizers.py

class SnippetSerializer(serializers.ModelSerializer):

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

当前 Swagger UI 的屏幕截图,因为没有标题参数和 post 参数的字段。

版本:

django-rest-swagger==2.1.0

djangorestframework==3.9.1

4

0 回答 0