搜索后台接口
一、流程
'''
# 后端
1. 模糊匹配: SearchFilter
2. 自己写: 自定义过滤类, 继承filter_queryset, 指定'字段__contains', return queryset
3. 搜索接口: 不和课程一起, 坨到一起了
CourseSearchView 继承 G/L
内部书写SearchFilter的即可
提示: 需要携带分页
4. 配置路由: search
'''
二、路由:course/urls.py
router.register('search',views.CourseSearchView,'search')
三、视图:course/views.py
from rest_framework.viewsets import GenericViewSet
from rest_framework.mixins import ListModelMixin
from rest_framework.filters import SearchFilter
from . import models, serializers
from .filters import CourseFilter
# 搜索课程接口
class SearchCourseViewSet(ListModelMixin, GenericViewSet):
queryset = Course.objects.filter(is_delete=False, is_show=True).order_by('orders')
serializer_class = CourseModelSerializer
# 提示: 需要携带分页
pagination_class = PageNumberPagination
# 自定义
# filter_backends = [CourseFilter]
# 使用SearchFilter
filter_backends = [SearchFilter]
search_fields = ['name']