django-rest-swagger
django-rest-swagger copied to clipboard
How to specify POST/PUT parameters in v2
Using generic api views works fine, the parameters are recognized from the serializer.
But what about views that don't have a serializer? What if I want to give a rating to an item for example.
It will be something like
curl -X PUT "/api/items/6/rate" -H "accept: application/json" -H "Content-Type: application/json" -d "{ \"rating\": 4}"
How do I specify this with django rest swagger? The docs don't help with this usecase, and old docs for v0.3 are not working anymore.
class RatingView(views.APIView):
def put(self, request, pk, format=None):
"""
Your docs
---
parameters:
- name: foobar
description: test
required: true
type: integer
paramType: query
"""
item = self.get_object(pk)
Ratings.rate(item, request.data.get("rating", 0))
return Response("ok")
The parameter does not show up.
I found an ugly workaround, but surely there is a better way:
def get_serializer(*_, **__):
class MySerializer(serializers.Serializer):
rating = serializers.IntegerField()
return MySerializer()
use coreapi
class S2ViewList(GenericAPIView):#
serializer_class = S2createSerializer #
schema = AutoSchema (
manual_fields=[
coreapi.Field (name='vxnet',required=False,location='query',description='',type='string'),
coreapi.Field (name='service_type',required=False,location='query',description='',
type='string'),
]
)
I found this in stackoverflow, and I hope it helps you.
https://stackoverflow.com/questions/38542690/django-rest-framework-swagger-2-0
i tried many way to solve this problem , now here is the solution