Karesh A
Karesh A

Reputation: 1751

Django Rest queryset inside APIview

I have two serializers for two models but I want to combine those two serializers into one view

class ProductRequestView(APIView):
    permission_classes = [IsAuthenticatedOrReadOnly]

    def get(self, request):

        city_serializer = CityCompactSerializer
        models = ModelsNestedSerializer

        data = {'cities':{'city data'},
                'models': {'models data'}}

        return Response(data, status=HTTP_200_OK)

I think I have to pass queryset to both serializers to get the data. How can I do it inside this view. I am new to DRF. Help ? Thanks

Upvotes: 1

Views: 2134

Answers (1)

trinchet
trinchet

Reputation: 6933

class ProductRequestView(APIView):
    permission_classes = [IsAuthenticatedOrReadOnly]

    def get(self, request):

        data = {
            'cities': CategoryCompactSerializer(<CategoryModel>.objects.all(), many=True).data
            'models': ModelsNestedSerializer(<ModelsModel>.objects.all(), many=True).data
        }

        return Response(data, status=HTTP_200_OK)

Upvotes: 3

Related Questions