安装
pip install django-filter
注册app
INSTALLED_APPS = [ ... 'django_filters', # 需要注册应用, ]
在视图中使用
from django_filters.rest_framework import DjangoFilterBackend
class CourseListApiView(ListAPIView): # 指定过滤类 filter_backends = [DjangoFilterBackend, ] # 指定过滤字段 filter_fields = ('course_category',) queryset = Course.objects.filter(is_show=True).order_by('orders', '-id') serializer_class = CourseSerializer
测试