Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filtering against query param

I created my "API" using REST framework, now I am trying to do filtering for it.

This is how my models.py looks like:

class Airline(models.Model):     name = models.TextField()  class Workspace(models.Model):     airline = models.ForeignKey(Airline)     name = models.CharField(max_length=100)  class Passenger(models.Model):     workspace = models.ForeignKey(Workspace)     title = models.CharField(max_length=200) 

I would like to see "all passengers in particular workspace" or "all passengers in particular airline" etc in my JSON file.

Here is my serializers.py:

class AirlineSerializer(serializers.ModelSerializer):     class Meta:         model = Airline   class WorkspaceSerializer(serializers.ModelSerializer):     class Meta:         model = Workspace   class PassengerSerializer(serializers.ModelSerializer):     class Meta:         model = Passenger 

And views.py:

class AirlineList(generics.ListCreateAPIView):     model = Airline     serializer_class = AirlineSerializer   class AirlineDetail(generics.RetrieveUpdateDestroyAPIView):     model = Airline     serializer_class = AirlineSerializer   class WorkspaceList(generics.ListCreateAPIView):     model = Workspace     serializer_class = WorkspaceSerializer   class WorkspaceDetail(generics.RetrieveUpdateDestroyAPIView):     model = Workspace     serializer_class = WorkspaceSerializer   class PassengerList(generics.ListCreateAPIView):     model = Passenger     serializer_class = PassengerSerializer   class PassengerDetail(generics.RetrieveUpdateDestroyAPIView):     model = Passenger     serializer_class = PassengerSerializer 

I would like to use Filtering against query parameter but I can't really get it...

like image 795
knowbody Avatar asked Jan 17 '14 09:01

knowbody


People also ask

What is DjangoFilterBackend?

The DjangoFilterBackend class is used to filter the queryset based on a specified set of fields. This backend class automatically creates a FilterSet (django_filters. rest_framework. FilterSet) class for the given fields. We can also create our own FilterSet class with customized settings.

How do I pass a URL filter?

The basic syntax is fairly straightforward; start with the report URL, add a question mark, and then add your filter syntax. Table and Field names are case-sensitive, value isn't. Fields that are hidden from report view can still be filtered.

How can I filter a Django query with a list of values?

To filter a Python Django query with a list of values, we can use the filter method with in . to search Blog entries with pk set to 1,4 or 7 by calling Blog. objects. filter with the pk_in argument set to [1, 4, 7] .


1 Answers

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 
like image 100
knowbody Avatar answered Sep 29 '22 20:09

knowbody