Customizing Filters in Django REST Framework
Last Updated :
17 Jul, 2024
Prerequisite: Adding Filtering in APIs – Django REST Framework [link needed article on published yet]
Django filters facilitate filtering the queryset to retrieve the relevant results based on the values assigned to the filter fields. But, what if the user wants to retrieve details within a given range. Say, for example, the user needs to fetch detail of robots based on price range. Here comes the necessity of customizing the filters. Let’s create and apply a customized filter to the Robot model so that the user can retrieve robot details by providing robot category name, manufacturer name, currency, manufacturing date range, and/or price range.
We will create a new class named RobotFilter class, which is a subclass of django_filters.FilterSet class. Let’s declare the imports
Python
from django_filters import FilterSet, AllValuesFilter
from django_filters import DateTimeFilter, NumberFilter
Now, you can add the below code before the RobotList class.
Python
class RobotFilter(FilterSet):
from_manufacturing_date = DateTimeFilter(field_name='manufacturing_date',
lookup_expr='gte')
to_manufacturing_date = DateTimeFilter(field_name='manufacturing_date',
lookup_expr='lte')
min_price = NumberFilter(field_name='price', lookup_expr='gte')
max_price = NumberFilter(field_name='price', lookup_expr='lte')
robotcategory_name = AllValuesFilter(field_name='robot_category__name')
manufacturer_name = AllValuesFilter(field_name='manufacturer__name')
class Meta:
model = Robot
fields = (
'name',
'currency',
'from_manufacturing_date',
'to_manufacturing_date',
'min_price',
'max_price',
'robotcategory_name',
'manufacturer_name',
)
Let’s look at the attributes declared in the RobotFilter class.
- from_manufacturing_date
- to_manufacturing_date
- min_price
- max_price
- robotcategory_name
- manufacturer_name
from_manufacturing_date: It is a django_filters.DateTimeFilter instance attribute that filters the robots whose manufacturing_date value is greater than or equal to the specified DateTime value. Here in the DateTimeFilter, there are two parameters named field_name and lookup_expr. The field_name has the manufacturing_date (for filtering), and ‘gte’ (greater than or equal to) is applied to the lookup_expr.
to_manufacturing_date: It is a django_filters.DateTimeFilter instance attribute that filters the robots whose manufacturing_date value is less than or equal to the specified DateTime value. Here in the DateTimeFilter, there are two parameters named field_name and lookup_expr. In the field_name, we mentioned the manufacturing_date, and ‘lte’ (less than or equal to) is applied to the lookup_expr.
min_price: It is a django_filters.NumberFilter instance attribute that filters the robots whose price value is greater than or equal to the specified price value.
max_price: It is a django_filters.NumberFilter instance attribute that filters the robots whose price value is less than or equal to the specified price value.
robotcategory_name: It is a django_filters.AllValuesFilter instance attribute that filters the robots whose robot category name matches with the specified string value. You can notice that there is a double underscore (__) in the value provided to the field_name, between robot_category and name. The field_name uses the Django double underscore to read it as the name field for the RobotCategory model. This helps to retrieve the robot’s detail based on the robot category name rather than its pk id.
manufacturer_name: It is a django_filters.AllValuesFilter instance attribute that filters the robots whose manufacturer name matches with the specified string value. The field_name uses the Django double underscore to read the value ‘manufacturer__name’ as the name field for the Manufacturer model. This helps to retrieve the robot’s detail based on the manufacturer name rather than its pk id.
The RobotFilter class also defines a Meta inner class. This class has two attributes model and fields. The model attribute specifies the model (Robot) to filter. And, the fields attribute holds field names and filter names (as a tuple of strings) to include in the filters for the mentioned model (Robot).
Let’s use the RobotFilter class in our RobotList class. The code as follows
Python
class RobotList(generics.ListCreateAPIView):
queryset = Robot.objects.all()
serializer_class = RobotSerializer
name = 'robot-list'
# customized filter class
filter_class = RobotFilter
search_fields = (
'^name',
)
ordering_fields = (
'price',
)
Let’s filter the robots within a manufacturing date. The HTTPie command is
http “:8000/robot/?from_manufacturing_date=2019-10-01&to_manufacturing_date=2020-03-01”
Output:

Let’s filter the robots based on the robot category name and manufacturer name. The HTTPie command as follows
http “:8000/robot/?robotcategory_name=Articulated Robots&manufacturer_name=Fanuc”
Output:

Let’s filter the robots based on price range. The HTTPie command is
http “:8000/robot/?min_price=10000&max_price=20000¤cy=USD”
Output:

Let’s filter the robots using the browsable API feature. You can browse the below URL and click the filter button.
http://127.0.0.1:8000/robot/
You can populate the values to filter against robots. Sharing the screenshot below

On clicking the submit button, you will get filtered results. Sharing the screenshot below

Similar Reads
Filter data in Django Rest Framework
Django REST Framework's generic list view, by default, returns the entire query sets for a model manager. For real-world applications, it is necessary to filter the queryset to retrieve the relevant results based on the need. So, let's discuss how to create a RESTful Web Service that provides filter
4 min read
Customizing Object Level Permissions - Django REST Framework
In this article, we will discuss how to customize Object Level Permissions in Django REST Framework. To customize permission classes in Django REST Framework, we should inherit the rest_framework.permissions.BasePermission class and implement either or both of the following methods: .has_permission(
5 min read
Custom Template Filters in Django
Django is a Python-based web framework that allows you to quickly create efficient web applications. It is also called batteries included framework because Django provides built-in features for everything including Django Admin Interface, default database â SQLlite3, etc. What is filters in Django t
2 min read
Django form field custom widgets
A widget is Djangoâs representation of an HTML input element. The widget handles the rendering of the HTML, and the extraction of data from a GET/POST dictionary that corresponds to the widget. Whenever you specify a field on a form, Django will use a default widget that is appropriate to the type o
3 min read
URL fields in serializers - Django REST Framework
In Django REST Framework the very concept of Serializing is to convert DB data to a datatype that can be used by javascript. Every serializer comes with some fields (entries) which are going to be processed. For example if you have a class with name Employee and its fields as Employee_id, Employee_n
5 min read
JWT Authentication with Django REST Framework
JSON Web Token is an open standard for securely transferring data within parties using a JSON object. JWT is used for stateless authentication mechanisms for users and providers, this means maintaining session is on the client-side instead of storing sessions on the server. Here, we will implement t
2 min read
How To Filter A Nested Serializer In Django Rest Framework?
When building APIs with Django Rest Framework (DRF), nested serializers are commonly used to represent relationships between models. A nested serializer allows us to include data from related models within a serializer. However, there are instances where we might want to filter the data returned by
6 min read
Basic Authentication - Django REST Framework
Authentication is a mechanism that provides access control based on the credentials associated with incoming requests. Django REST Framework provides several authentication schemes. In this section, let's look at the Basic Authentication in Django rest framework, i.e., authenticated against a user's
4 min read
Choice Selection Fields in serializers - Django REST Framework
In Django REST Framework the very concept of Serializing is to convert DB data to a datatype that can be used by javascript. Every serializer comes with some fields (entries) which are going to be processed. For example if you have a class with name Employee and its fields as Employee_id, Employee_n
5 min read
Browsable API in Django REST Framework
The browsable API feature in the Django REST framework generates HTML output for different resources. It facilitates interaction with RESTful web service through any web browser. To enable this feature, we should specify text/html for the Content-Type key in the request header. It helps us to use we
8 min read