Working with different types of Django filters
Now, we will create a customized filter that we will apply to the Competition
model. We will code the new CompetitionFilter
class, specifically, a subclass of the rest_framework.filters.FilterSet
class.
Open the restful01/drones/views.py
file. Add the following code before the declaration of the CompetitionList
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 CompetitionFilter(filters.FilterSet): from_achievement_date = DateTimeFilter( name='distance_achievement_date', lookup_expr='gte') to_achievement_date = DateTimeFilter( name='distance_achievement_date', lookup_expr='lte') min_distance_in_feet = NumberFilter( name='distance_in_feet', lookup_expr='gte') max_distance_in_feet = NumberFilter( name='distance_in_feet', lookup_expr='lte') drone_name = AllValuesFilter( name='drone__name') ...