2

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 Answer 1

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', )
Sign up to request clarification or add additional context in comments.

12 Comments

I am trying to use it without results. I added an example GenericAPIView to the first post. Can you show me how it should be done on this example and I will map it to the rest of my views?
I added a sample for your view. You should first install pip install django-filter and add 'django_filters' to your INSTALLED_APPS
I have tried your genericAPIView but it's not visible in Swagger. It doesn't return anything. Are you sure this is equivalent of my view from the first post?
sorry my bad, you should subclass ListAPIView . You should also have an url pointing to this view.
Unfortunately after changes I get error AssertionError at /object-data 'ObjectList' should either include a queryset` attribute, or override the get_queryset() method.` I have added my URL to the first post.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.