Django REST framework - filtering against query param

前端 未结 3 613
甜味超标
甜味超标 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:31

    You can get the same functionality out of the box just by using django-filter package as stated in the docs http://www.django-rest-framework.org/api-guide/filtering/#djangofilterbackend

    from rest_framework import filters 
    
    class PassengerList(generics.ListCreateAPIView):
        model = Passenger
        serializer_class = PassengerSerializer
        queryset = Passenger.objects.all()
        filter_backends = (filters.DjangoFilterBackend,)
        filter_fields = ('workspace', 'workspace__airline')
    

    In this case you will have to make filtering using 'workspace=1' or 'workspace__airline=1'

提交回复
热议问题