jsondjangorestdjango-rest-frameworkdrf-spectacular

drf-spectacular define request schema as JSON Array (like Serializer(many=True))


Is it possible to define "many" serializer schema in drf-spectacular?

The request should take this data (JSONArray):

MonthlyIncomeSerializer(many=True)

Which is a list of objects/dictionaries:

[
    {'year':..., 'month':..., 'amount': ...},
    {'year':..., 'month':..., 'amount': ...},
    {'year':..., 'month':..., 'amount': ...},
]

I tried:

class PartialDTIPrenajomView(APIView):

    @extend_schema(parameters=[MonthlyIncomeSerializer(many=True)])
    def post(self, request, **kwargs):

which doesn't render anything in Swagger.


Solution

  • extend_schema's parameters argument is used for query parameters therefore it doesn't show anything on the POST method.

    Changing to use the request argument should solve the issue.

    class PartialDTIPrenajomView(APIView):
    
        @extend_schema(request=MonthlyIncomeSerializer(many=True))
        def post(self, request, **kwargs):
            ...