In This Tutorial we will learn about filter , search and ordering in django rest framework .
1. Filtering
The simplest way to filter the queryset of any view that subclass GenericAPIView is to override the get_queryset() method.
from rest_framework.generics import ListAPIView
class EmployeeListAPIView(ListAPIView):
queryset=Employee.objects.all()
serializer_class=EmployeeSerializer
def get_queryset(self):
return Employee.objects.filter(age__gt=50)
We have concept of Generic Filtering To filter queryset in django rest framework
REST Framework also include support for generic filtering backends that allow you to easily construct complex search and filters.
DjangoFilterBackend
The django-filter library includes a DjangoFilterBackend class which support high customable field filtering for Rest Framework.
To use DjangoFilterBackend , follow steps
1. pip install django_filter
2. Add "django_filters" to Django's INSTALLED_APPS
3.You can set the DjangoFilterBackend in settings.py file for the global setting or you can mention on individual view as well
# settings.py
REST_FRAMEWORK = {
'DEFAULT_FILTER_BACKENDS': ['django_filters.rest_framework.DjangoFilterBackend']
}
If all you need is simple equality-based filtering, you can set a filterset_fileds attribute in view or viewset , listing the set of fields you wish to filter against.
from rest_framework.generics import ListAPIView
from django_filters.rest_framework import DjangoFilterBackend
class EmployeeListAPIView(ListAPIView):
queryset=Employee.objects.all()
serializer_class=EmployeeSerializer
filter_backends=[DjangoFilterBackend]
filterset_fields=['name','age']
def filter_queryset(self, queryset):
return super().filter_queryset(queryset)
Then , client can filter by writing url like this
http://127.0.0.1:3000/mylist/?name=amrit&age=12
2. Search Filter
The Search filter class support simple sigle query parameter based searching and is based on the django admin's search functionalty.
The searchfilters class will only be applied if the view has search_fileds attribute set. The search_fileds attribute should be a list of names of text type fields on the model such as charfield, textfield e.t.c
from rest_framework.generics import ListAPIView
from rest_framework.filters import SearchFilter
class EmployeeListAPIView(ListAPIView):
queryset=Employee.objects.all()
serializer_class=EmployeeSerializer
filter_backends=[SearchFilter]
search_fields=['=name','age','company__name']
Then , client can search by writing url like this
http://127.0.0.1:3000/mylist/?search=amrit
If you want ot change the search keyword from query_params then you can achieve it through it
# settings.py
REST_FRAMEWORK = {
'SEARCH_PARAM':"q"
}
If you want more specific search , you can apply this symbol as well
"^" - start with search
"=" - exact match
'@" -full text search ( currently only support Djnago's PostgreSQL backend)
"$" - Regex search
3. Ordering Filter
The Ordering filter class support simple query parameter controlled ordering of result.
It's is recommanded that you explicitly specify which fileds the API should allowing in the ordering filter. you can do this by setting in ordering_fields attribute on the view like so:
from rest_framework.generics import ListAPIView
from rest_framework.filters import OrderingFilter
class EmployeeListAPIView(ListAPIView):
queryset=Employee.objects.all()
serializer_class=EmployeeSerializer
filter_backends=[OrderingFilter]
# ordering_fields=['name','age']
ordering_fields="__all__"
Then client can hit the url like this
http://127.0.0.1:3000/mylist/?ordering=-name
Multiply ordering may also be specified
http://127.0.0.1:3000/mylist/?ordering=-name,age
Amanda Martines 5 days ago
Exercitation photo booth stumptown tote bag Banksy, elit small batch freegan sed. Craft beer elit seitan exercitation, photo booth et 8-bit kale chips proident chillwave deep v laborum. Aliquip veniam delectus, Marfa eiusmod Pinterest in do umami readymade swag. Selfies iPhone Kickstarter, drinking vinegar jean.
ReplyBaltej Singh 5 days ago
Drinking vinegar stumptown yr pop-up artisan sunt. Deep v cliche lomo biodiesel Neutra selfies. Shorts fixie consequat flexitarian four loko tempor duis single-origin coffee. Banksy, elit small.
ReplyMarie Johnson 5 days ago
Kickstarter seitan retro. Drinking vinegar stumptown yr pop-up artisan sunt. Deep v cliche lomo biodiesel Neutra selfies. Shorts fixie consequat flexitarian four loko tempor duis single-origin coffee. Banksy, elit small.
Reply