How to use query parameters in Django Swagger Documentation?

℡╲_俬逩灬. 提交于 2019-12-12 05:48:53

问题


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.


回答1:


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', )


来源:https://stackoverflow.com/questions/45222078/how-to-use-query-parameters-in-django-swagger-documentation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!