The filter assembly
Sorting search filter group
drf written
first step
from rest_framework.filters import OrderingFilter
Step
local configuration the filter
filter_backends = [OrderingFilter,]
from the sort fields: ordering = -price, id
ordering_fields = ['price', 'id', 'students']
The third step is
a request linked style
http://127.0.0.1:8080?ordering=-price,id
price Sort done in accordance with the same sort id
Custom filters check group (limiting the number of data)
The first step
in app created folder below to create a filters.py
file name casually
The second step
from rest_framework.filters import BaseFilterBackend
rewrite class inheritance BaseFilterBackend
, override the method filter_queryset
from rest_framework.filters import BaseFilterBackend
class LimitFilter(BaseFilterBackend):
def filter_queryset(self, request, queryset, view):
li = request.query_params.get('li')
try:
# 取得到返回的是处理过后的queryset, 但是li可能是不是数字
return queryset[:int(li)]
except:
# 取不到返回原来的
return queryset
The third step is
to import the custom filter
from .filters import LimitFilter
The fourth step
configure a filter
filter_backends = [LimitFilter,]
Search group search filter
first step
from rest_framework.filters import SearchFilter
Step
configure a filter
filter_backends = [SearchFilter,]
The third step is
to configure a searchable word
search_fields = ['name', 'brief']
search solve is full-text search (that is 当前接口的序列化类连接的表中的字段
)
even fields of the table can not be queried
The fourth step
request link style
http://127.0.0.1:8080?search=python
Filters group search page
A class can only configure a pager
first step
in app created folder below to create a paginations.py
file name casually
The second step
from rest_framework.pagination import PageNumberPagination, LimitOffsetPagination, CursorPagination
rewrite class inheritance
BaseFilterBackend(普通分页器)
,LimitOffsetPagination(偏移分页器)
,CursorPagination(排序分页器)
Method filter_queryset rewrite
the code:
from rest_framework.pagination import PageNumberPagination, LimitOffsetPagination, CursorPagination
# 普通分页器
class CoursePageNumberPagination(PageNumberPagination):
# 默认一页条数
page_size = 2
# 选择哪一页的key
page_query_param = 'page'
# 一般不开放自定义
# 用户自定义一页条数
page_size_query_param = 'page_size'
# 用户自定义一页最大控制条数
max_page_size = 10
# 偏移分页器(可以从第几条加载到第几条)
class CourseLimitOffsetPagination(LimitOffsetPagination):
# 默认一页条数
default_limit = 2
# 从offset开始往后显示limit条
limit_query_param = 'limit'
offset_query_param = 'offset'
max_limit = 2
# 排序分页器(按照规则排序并且分页)
class CourseCursorPagination(CursorPagination):
cursor_query_param = 'cursor'
page_size = 2
page_size_query_param = 'page_size'
max_page_size = 2
# ordering = 'id' # 默认排序规则,不能和排序过滤器OrderingFilter共存
The third step is
to import the custom finisher
from .paginations import CoursePageNumberPagination, CourseLimitOffsetPagination, CourseCursorPagination
The fourth step
configure a custom finisher (the same below the interface can be configured with a pager)
pagination_class = CoursePageNumberPagination
pagination_class = CourseLimitOffsetPagination
pagination_class = CourseCursorPagination
Sort Filter
Django-filter plug-in installation
You need to install django-filter
the plug-
pip install django-filter
django-filter inside rewrite django
and rest_framework
the filter
front and rear end means that either do not separate classification screening can also be done before and after the end of the separation of Sort.
Classification using filters
The first step in
installing django-filter
the module
The second step
guide package
from django_filters.rest_framework import DjangoFilterBackend
Step
Configuration classification filters
filter_backends = [DjangoFilterBackend,]
The fourth step
configuration classification field (table must be serialized, some fields may be foreign key field)
filter_fields = ['course_category']
The fifth step
request link style
http://127.0.0.1:8080?course_category=2
Interval Filter Filters
Based on django-filter
plug-in, complete the designated section screening (usually for the digital field)
The first step
in app created folder below to create a filters.py
file name casually
The second step
introducing the desired module
from django_filters.rest_framework.filterset import FilterSet
from django_filters.rest_framework import filters
inherits FilterSet
similar to the write sequence class rewritten
NumberFilter
: providing data type filters, there are many types of data corresponding to
lte
: less than or equal
gte
: greater than or equal
field_name
: specified field can be classified section, and must be a sequence class fields in a sequence of
codes:
from django_filters.rest_framework.filterset import FilterSet
from django_filters.rest_framework import filters
from . import models
class CourseFilterSet(FilterSet):
# 价格小于指定价格(lte)
max_price = filters.NumberFilter(field_name='price', lookup_expr='lte')
# 价格大于指定价格(gte)
min_price = filters.NumberFilter(field_name='price', lookup_expr='gte')
class Meta:
model = models.Course
fields = ['price', 'max_price', 'min_price']
The third step
guide packet
from django_filters.rest_framework import DjangoFilterBackend
import custom class classification filter
from .filters import CourseFilterSet
configuration filter class
filter_backends = [DjangoFilterBackend]
configuration
filter_fields = ['course_category']
- django_filter help us write
filter_class = CourseFilterSet
- our own self-classification defined filter