Challenges Accessing List Action in Class Viewset with Retrieve-only Endpoint

63 views Asked by At

I have a class viewset that only performs the retrieve action. I have a challenge trying to access the endpoint if a retrieve action is not included in the viewset. The URL for this endpoint takes in an ID number, which is getting passed to the viewset. When a request is sent to this endpoint, the retrieve action is performed. I want to execute the list action, but I have failed to access it.

This is my Router

combineddata_router = routers.DefaultRouter()
combineddata_router.register('combined_data', ClientCombinedDataView, basename='combineddata')

This is my ViewSet

class ClientCombinedDataView(viewsets.ViewSet):
    def list(self, request, id):
        try:
            client_data = ClientModel.objects.get(pk=id)
            first_medical = FirstMedicalModel.objects.filter(pk=id)
            passport = ClientPpDetails.objects.get(client__id=id)
            nok = NokModel.objects.get(client__id=id)

            serializer = CombinedDataSerializer({
                "client": client_data,
                "first_medical": first_medical,
                "passport": passport,
                "nok": nok
            })

            data = {
                "status": status.HTTP_200_OK,
                "message": "combined Data returned",
                "data": serializer.data
            }

            return Response(data)
        except:

            return Response(status=status.HTTP_400_BAD_REQUEST)
    


    def retrieve(self, request, pk=None):
        client = get_object_or_404(ClientModel,pk=pk)
        serializer = ClientModelSerializer(client)
        return Response(serializer.data, status=status.HTTP_200_OK)

I expected http://127.0.0.1:8000/combined_data/2/ to perform the the list action but instead it performs the retrieve. How can I modify my code to be able to perform the list action

0

There are 0 answers