官方文档:https://www.django-rest-framework.org/api-guide/filtering/
DjangoFilterBackend
官方文档:https://django-filter.readthedocs.io/en/master/
本身支持模糊搜索和自定义,但是建议使用精确搜索,过滤的定义更多偏向于精确,和mixins.ListModelMixin, viewsets.GenericViewSet配合使用
from django_filters.rest_framework import DjangoFilterBackend精确过滤,参照官方例子
示例:精确过滤mock类型
views.py文件
from django_filters.rest_framework import DjangoFilterBackend
class MockTaskDetailViewSet(mixins.ListModelMixin, viewsets.GenericViewSet):
"""
创建、更新、删除MOCK任务详情
"""
queryset = PaymentMockTask.objects.all()
serializer_class = MockTaskListSerializer
filter_backends = (DjangoFilterBackend, ) # 本身使用api_settings.DEFAULT_FILTER_BACKENDS,需重写filter_backends
filterset_fields = ['mock_type', ] # DjangoFilterBackend精确过滤关键字和数据库字段相同
django_filters.rest_framework.FilterSet
自定义过滤:参照官方例子
不能同时声明
示例:精确过滤mock类型、mock环境、mock上传状态、mock状态
新建filter.py文件
import django_filters
from .models import PaymentMockTask
class MockTaskDetailFilter(django_filters.rest_framework.FilterSet):
"""
MOCK详情的过滤类
"""
mockType = django_filters.CharFilter(field_name='mock_type', help_text="mock类型", lookup_expr='exact') #lookup_expr为ORM
mockEnv = django_filters.NumberFilter(field_name='mock_env', help_text="mock环境", lookup_expr='exact')
uploadStatus = django_filters.NumberFilter(field_name='upload_status', help_text="上传状态", lookup_expr='exact')
mockStatus = django_filters.NumberFilter(field_name='mock_status', help_text="mock状态", lookup_expr='exact')
class Meta:
model = PaymentMockTask
fields = ['mockType', 'mockEnv', 'uploadStatus', 'mockStatus']
views.py文件
from .filters import MockTaskDetailFilter
class MockTaskDetailViewSet(mixins.ListModelMixin, viewsets.GenericViewSet):
"""
创建、更新、删除MOCK任务详情
"""
queryset = PaymentMockTask.objects.all()
serializer_class = MockTaskListSerializer
filter_backends = (DjangoFilterBackend, ) # 本身使用api_settings.DEFAULT_FILTER_BACKENDS,需重写filter_backends
filterset_class = MockTaskDetailFilter
SearchFilter,多应用于模糊搜索场景,支持模糊搜索(默认)、精确搜索、全文搜索、正则表达式搜索
官方文档:https://www.django-rest-framework.org/api-guide/filtering/#searchfilter
参考官方示例
示例:mock任务名称模糊搜索
views.py文件
from rest_framework import filters
class MockTaskDetailViewSet(mixins.ListModelMixin, viewsets.GenericViewSet):
"""
创建、更新、删除MOCK任务详情
"""
queryset = PaymentMockTask.objects.all()
serializer_class = MockTaskListSerializer
filter_backends = (filters.SearchFilter, ) # 本身使用api_settings.DEFAULT_FILTER_BACKENDS,需重写filter_backends
search_fields = ('task_name', ) #指定搜索字段和条件
默认前端传search=xxx搜索
主要希望不使用默认的search
官方解释允许定义key
By default, the search parameter is named 'search', but this may be overridden with the SEARCH_PARAM setting.
示例:mock任务名称模糊搜索,把search替换成task_name
新建filter.py
from rest_framework import filters
class MockTaskDetailSearchFilter(filters.SearchFilter):
"""
MOCK详情的搜索类
"""
search_param = "task_name" #把search替换成task_name
views.py文件
from .filters import MockTaskDetailFilter
class MockTaskDetailViewSet(mixins.ListModelMixin, viewsets.GenericViewSet):
"""
创建、更新、删除MOCK任务详情
"""
queryset = PaymentMockTask.objects.all()
serializer_class = MockTaskListSerializer
filter_backends = (MockTaskDetailSearchFilter, ) # 本身使用api_settings.DEFAULT_FILTER_BACKENDS,需重写filter_backends
search_fields = ('task_name', ) #指定搜索字段和条件
OrderingFilter
官方文档:https://www.django-rest-framework.org/api-guide/filtering/#orderingfilter
参考官方
示例:任务id排序
views.py文件
from rest_framework import filters
class MockTaskDetailViewSet(mixins.ListModelMixin, viewsets.GenericViewSet):
"""
创建、更新、删除MOCK任务详情
"""
queryset = PaymentMockTask.objects.all()
serializer_class = MockTaskListSerializer
filter_backends = (filters.OrderingFilter, ) # 本身使用api_settings.DEFAULT_FILTER_BACKENDS,需重写filter_backends
ordering_fields = ('id',)
默认key是ordering
主要希望不使用默认的ordering
官方解释允许定义key
By default, the query parameter is named 'ordering', but this may by overridden with the ORDERING_PARAM setting.
示例:任务id排序,把ordering替换成id_ordering
新建filter.py
from rest_framework import filters
class MockTaskDetailOrderingFilter(filters.OrderingFilter):
"""
MOCK详情的排序类
"""
ordering_param = "id_ordering"
ordering_fields = ('id',)
views.py文件
from rest_framework import filters
class MockTaskDetailViewSet(mixins.ListModelMixin, viewsets.GenericViewSet):
"""
创建、更新、删除MOCK任务详情
"""
queryset = PaymentMockTask.objects.all()
serializer_class = MockTaskListSerializer
filter_backends = (MockTaskDetailOrderingFilter, ) # 本身使用api_settings.DEFAULT_FILTER_BACKENDS,需重写filter_backends