过滤模块
安装
>: pip install django-filter
注册应用:settings/dev.py
INSTALLED_APPS = [
'django_filters',
]
条件过滤
视图函数:course/views.py
from django_filters.rest_framework import DjangoFilterBackend
class CourseAPIView(ListAPIView):
"""课程列表接口"""
queryset = Course.objects.filter(is_delete=False, is_show=True).order_by("-orders")
serializer_class = CourseSerializer
filter_backends = [DjangoFilterBackend]
filter_fields = ['course_category', ]
接口
http://127.0.0.1:8000/course/?course_category=1
排序过滤
视图函数:course/views.py
from rest_framework.filters import OrderingFilter
from .pagination import CoursePageNumberPagination
class CourseAPIView(ListAPIView):
"""课程列表接口"""
queryset = Course.objects.filter(is_delete=False, is_show=True).order_by("-orders")
serializer_class = CourseSerializer
filter_backends = [OrderingFilter]
ordering_fields = ['id', 'students', 'price']
接口
http:
http: