Justin-刘清政的博客

python/路飞项目/7-搜索功能/2-搜索后台接口

2020-03-06

路由:course/urls.py
1
router.register('search', views.SearchCourseViewSet, 'search')  # 搜索课程
视图:course/views.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
from rest_framework.viewsets import GenericViewSet
from rest_framework.mixins import ListModelMixin
from rest_framework.filters import SearchFilter
from . import models, serializers

# 搜索课程接口
class SearchCourseViewSet(GenericViewSet, ListModelMixin):
queryset = models.Course.objects.filter(is_delete=False, is_show=True).all()
serializer_class = serializers.CourseSerializer

pagination_class = pagination.PageNumberPagination

filter_backends = [SearchFilter]
search_fields = ['name']
使用支付宝打赏
使用微信打赏

点击上方按钮,请我喝杯咖啡!

扫描二维码,分享此文章