Adding filtering, searching, and ordering

Now, we will add the necessary code to configure the fields that we want to be included in the filtering, searching, and ordering features for each of the class-based views that retrieve the contents of each resource collection. Hence, we will make changes to all the classes with the List suffix in the views.py file: DroneCategoryList, DroneList, PilotList, and CompetitionList.

We will declare the following three class attributes in each of those classes:

Open the restful01/drones/views.py file. Add the following code after the last line that declares the imports, before the declaration of the DroneCategoryList class. The code file for the sample is included in the hillar_django_restful_07_03 folder in the restful01/drones/views.py file:

from rest_framework import filters 
from django_filters import AllValuesFilter, DateTimeFilter, NumberFilter 

Add the following highlighted lines to the DroneList class declared in the views.py file. The next lines show the new code that defines the class. The code file for the sample is included in the hillar_django_restful_07_03 folder in the restful01/drones/views.py file:

class DroneCategoryList(generics.ListCreateAPIView): 
    queryset = DroneCategory.objects.all() 
    serializer_class = DroneCategorySerializer 
    name = 'dronecategory-list' 
    filter_fields = ( 
        'name', 
        ) 
    search_fields = ( 
        '^name', 
        ) 
    ordering_fields = ( 
        'name', 
        ) 

The changes in the DroneList class are easy to understand. We will be able to filter, search, and order by the name field.

Add the following highlighted lines to the DroneList class declared in the views.py file. The next lines show the new code that defines the class. The code file for the sample is included in the hillar_django_restful_07_03 folder in the restful01/drones/views.py file:

class DroneList(generics.ListCreateAPIView): 
    queryset = Drone.objects.all() 
    serializer_class = DroneSerializer 
    name = 'drone-list' 
    filter_fields = ( 
        'name',  
        'drone_category',  
        'manufacturing_date',  
        'has_it_competed',  
        ) 
    search_fields = ( 
        '^name', 
        ) 
    ordering_fields = ( 
        'name', 
        'manufacturing_date', 
        ) 

In the DroneList class, we specified many field names in the filter_fields attribute. We included 'drone_category' in the string tuple, and therefore, we will be able to include the ID values for this field in the filter.

We will take advantage of other options for related models that will allow us to filter by fields of the related model later. This way, we will understand the different customizations available.

The ordering_fields attribute specifies two field names for the tuple of strings, and therefore, we will be able to order the results by either name or manufacturing_date. Don't forget that we must take into account database optimizations when enabling fields to order by.

Add the following highlighted lines to the PilotList class declared in the views.py file. The next lines show the new code that defines the class. The code file for the sample is included in the hillar_django_restful_07_03 folder in the restful01/drones/views.py file:

class PilotList(generics.ListCreateAPIView): 
    queryset = Pilot.objects.all() 
    serializer_class = PilotSerializer 
    name = 'pilot-list' 
    filter_fields = ( 
        'name',  
        'gender', 
        'races_count', 
        ) 
    search_fields = ( 
        '^name', 
        ) 
    ordering_fields = ( 
        'name', 
        'races_count' 
        )

The ordering_fields attribute specifies two field names for the tuple of strings, and therefore, we will be able to order the results by either name or races_count.