문제

Using the Django-Rest-Framework, I have successfully built APIs that return my application's models. You can see the models, viewsets, serializers etc here.

Now I want to create another, much simpler API. This api will not return a list of model objects. Instead it will return a single integer. So the viewset will looks something like this:

class MyObjectCount(viewsets.ViewSet):
    def retrieve(self,request,pk=None):
        queryset = 21
        serializer = IntegerSerializer(queryset)
        return Response(serializer.data,status=status.HTTP_200_OK)

But I don't know what Integer Serializer should look like. Or maybe I don't need to write my own IntegerSerializer but can instead can use some default serializer for such cases. Can someone please recommend the best solution?

올바른 솔루션이 없습니다

다른 팁

You only need serializers for Django model objects because standard per-format libraries don't know how to handle Django objects. 'Primitive' Python native types are understood, just return those natively and have the Django Rest framework take care of turning that into JSON for you (based on content negotiation.

return Response(queryset)
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top