Request & Response schema of django-rest-swagger for documenting the API in DRF

5.9k Views Asked by At

I want to document my API using swagger generator tool django-rest-swagger in DRF

right now I am writing views by inheriting rest_framework.views.APIView

I don't want to write views using viewsets nor serializer.

Here is the view code sample

from rest_framework.views import APIView
class SomeView(APIView):
  '''
  get:
    some description
  post:
    some other description
 '''
 def get(self, request, format=None):
    a = self.request.query_params.get('a',None)
    b = self.request.query_params.get('b',None)
    c = self.request.query_params.get('c',None)
    return Response({},status='200')

 def post(self, request, format=None):
    a = self.request.data.get('a',None)
    b = self.request.data.get('b',None)
    c = self.request.data.get('c',None)
    return Response({},status='201')

Right now I am able to add description for each endpoint. description for each api endpoint in swagger ui

and I want to add request & response schema like below model schema for each endpoint in swagger

and I am wondering, how to achieve this without using serializers & viewsets.

2

There are 2 best solutions below

0
On

I know that i'm not answering directly to your question, but i would like to suggest you to try an awesome package that is customizable in every direction: drf-yasg. You are not obligated to use generic rest_framework stuff nor serializers.

You can jump directly to Custom schema generation

2
On

In the current version of django-rest-swagger YAML docstrings (as in your example) have been deprecated I believe. Accordingly, I think you would need to manually define your schema. The docs include an example of how to do this on the view level:

http://www.django-rest-framework.org/api-guide/schemas/#manualschema