Post body parameter in Django Rest Framework Swagger

5.2k views Asked by At

I would like to add Swagger to a Django rest framework, but I can't get POST body parameters...

What I do:

serrings.py

INSTALLED_APPS = [
   ....
   'rest_framework_swagger',
]


REST_FRAMEWORK = {
   'DEFAULT_PARSER_CLASSES': [
       'rest_framework.parsers.FormParser',
       'rest_framework.parsers.MultiPartParser',
       'rest_framework.parsers.JSONParser',
   ]
}

urls.py

schema_view = get_swagger_view(title='My API')
urlpatterns = [
    path('', schema_view),
    ...
]

Views.py

class MyView(APIView):

    def post(self, request, format=None):
        ...
        return Response(something)

But my swagger always looks like :

enter image description here

In this case I can't post my body parameter (json) to try it out... How to fix this

2

There are 2 answers

5
aman kumar On

swagger picks the fields name from serializer class, so you have to add the serializer class name in view.

class MyView(APIView):
    serializer_class = SerializerclassName
    def post(self, request, format=None):
        ...
        return Response(something)
1
MyounghoonKim On

You need to add serializer_class. For example,

class PostSomething(generics.CreateAPIView):
    serializer_class = rest_framework.serializers.Serializer # here
    permission_classes = (AllowAny,)
    allowed_methods = ('POST',)