Django REST framework - filtering against query param

前端 未结 3 615
甜味超标
甜味超标 2020-12-23 00:17

So I created my \"API\" using REST framework, now trying to do filtering for it. That\'s how my models.py look like more or less:

class Airline(         


        
3条回答
  •  太阳男子
    2020-12-23 00:25

    So with the @limelights I managed to do what I wanted, here is the code:

    class PassengerList(generics.ListCreateAPIView):
        model = Passenger
        serializer_class = PassengerSerializer
    
        # Show all of the PASSENGERS in particular WORKSPACE
        # or all of the PASSENGERS in particular AIRLINE
        def get_queryset(self):
            queryset = Passenger.objects.all()
            workspace = self.request.query_params.get('workspace')
            airline = self.request.query_params.get('airline')
    
            if workspace:
                queryset = queryset.filter(workspace_id=workspace)
            elif airline:
                queryset = queryset.filter(workspace__airline_id=airline)
    
            return queryset
    

提交回复
热议问题