How to use query parameters in Django Swagger Docu

2019-08-26 05:06发布

Let's assume that we have for example such URL - localhost:8000/object?name=STH. Anyone have an idea how can I display object with name equals STH using Django Swagger Documentation?

class ObjectList(GenericAPIView):

    serializer_class = ObjectSerializer

    def get(self, request):
        try:
            t = request.GET['sth']
            object = Object.objects.filter(sth=sth)
        except:
            object = Object.objects.all()
        serializer = ObjectSerializer(object, many=True)
        return Response(serializer.data)

I am trying in this way but it's not visible in my Swagger:

URL: url(r'^object-data$', views.ObjectList.as_view()),

class ObjectList(GenericAPIView):
    serializer_class = ObjectSerializer
    filter_backends = (DjangoFilterBackend, )
    filter_fields = ('sth', )

At this moment my Swagger looks as shown below. I would like to have sth instead of pages. I have no idea why it is there. enter image description here

1条回答
男人必须洒脱
2楼-- · 2019-08-26 05:51

I am using djang-filter package that integrates nicely with rest framework and it also has support for swagger docs, you get auto-generated filtering columns for that endpoint in swagger.

from django_filters.rest_framework import DjangoFilterBackend
from rest_framework.generics import ListAPIView

class ObjectList(ListAPIView):
    serializer_class = ObjectSerializer
    queryset = Object.objects.all()
    filter_backends = (DjangoFilterBackend, )
    filter_fields = ('sth', )
查看更多
登录 后发表回答