This is the 18th day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021

filter

Filtration installation

Filtering is not built into Django rest_framwork and requires third-party packages to be installed

pip install django-filter

Note: Django-filter only works with Django 2.2 or later. If you have Django installed on your computer earlier than 2.2, install django-Filter It will automatically uninstall Django from your computer and reinstall the latest version of Django and django-Filter

Basic use of filtering

Filtering is to filter the results on the premise of obtaining all the information, so the view class only needs to achieve the function of obtaining all the data, that is, the view class inherits ListAPIView.

Is the filtering mode in routing? Field = Filter criteria

Note:

Filtering will only work if you inherit GenericAPIView or other subclasses that inherit GenericAPIView or extend classes that need to match GenericAPIView. If you define methods to get all the data in your view class, the filter will not work because there is no internal filter_queryset method.

  • You need to register your application
INSTALLED_APPS = [
    ...
    'django_filters'.You need to register your app.
]
Copy the code
  • Global/local configuration
# global configuration
REST_FRAMEWORK = {
    ...
    'DEFAULT_FILTER_BACKENDS': ('django_filters.rest_framework.DjangoFilterBackend')},# local configuration
from django_filters.rest_framework import DjangoFilterBackend
filter_backends = [DjangoFilterBackend,]
Copy the code
  • Add the filter_fields attribute to the view to specify the fields that can be filtered
class StudentListView(ListAPIView) :
    queryset = Student.objects.all()
    serializer_class = StudentSerializer
    filter_fields = ('age'.'sex')

# 127.0.0.1:8000 / four/students /? sex=1
Copy the code

The code examples

  • Global configuration
'DEFAULT_FILTER_BACKENDS': ['django_filters.rest_framework.DjangoFilterBackend'].Copy the code
  • The view class
from rest_framework.generics import ListAPIView
class BookView(ListAPIView) :
    queryset = models.Book.objects.all()
    serializer_class = BookModelSerializer
    filter_fields = ('name'.'price')
Copy the code

The sorting

For list data, the REST Framework provides an OrderingFilter to help us quickly indicate that the data is sorted by the specified field.

Usage:

Set filter_backends in class view, use rest_framework. Filters. OrderingFilter filter, REST framework will be in the query string parameters of request to check whether contains ordering parameters, If the ordering parameter is included, the data set is ordered according to the ordering field specified by the ordering parameter.

The optional field values of the ordering parameter that can be passed by the front end need to be specified in ordering_fiel DS.

class StudentListView(ListAPIView) :
    queryset = Student.objects.all()
    serializer_class = StudentModelSerializer
    filter_backends = [OrderingFilter]
    ordering_fields = ('id'.'age')

# 127.0.0.1:8000 / books /? ordering=-age
# -id: sort the id field in reverse order
# id indicates an ascending sort for the ID field
Copy the code

If you need to sort again after filtering, you need a combination of both!

from rest_framework.generics import ListAPIView
from students.models import Student
from .serializers import StudentModelSerializer
from django_filters.rest_framework import DjangoFilterBackend

class Student3ListView(ListAPIView) :
    queryset = Student.objects.all()
    serializer_class = StudentModelSerializer
    filter_fields = ('age'.'sex')
    The fields used in local configuration sorting are the same as those used in filtering, and local configuration overwrites global configuration, so you need to redeclare the filtering component, otherwise the filtering component will fail.
    filter_backends = [OrderingFilter,DjangoFilterBackend]
    ordering_fields = ('id'.'age')
Copy the code

conclusion

The article was first published in the wechat public account Program Yuan Xiaozhuang, at the same time in nuggets.

The code word is not easy, reprint please explain the source, pass by the little friends of the lovely little finger point like and then go (╹▽╹)