区间过滤
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
# 借助django-filter实现区间过滤 # 实现区间过滤 ##########1 filters.py class CourseFilterSet(FilterSet): # 课程的价格范围要大于min_price,小于max_price min_price = filters.NumberFilter(field_name='price', lookup_expr='gt') max_price = filters.NumberFilter(field_name='price', lookup_expr='lt') class Meta: model=models.Course fields=['course_category'] #####2 视图类中配置 -filter_backends=[DjangoFilterBackend] # 配置类:(自己写的类) -filter_class = CourseFilterSetView Code
课程单查接口
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
class CouresView(GenericViewSet,ListModelMixin,RetrieveModelMixin): queryset = models.Course.objects.filter(is_delete=False,is_show=True).order_by('orders') serializer_class = serializer.CourseModelSerializer pagination_class = PageNumberPagination # 过滤和排序 # filter_backends=[DjangoFilterBackend,OrderingFilter,SearchFilter] # filter_backends=[DjangoFilterBackend,OrderingFilter,MyFilter] filter_backends=[DjangoFilterBackend,OrderingFilter] # # filter_backends=OrderingFilter ordering_fields=['id', 'price', 'students'] # # search_fields=['course_category'] filter_fields=['course_category']View Code
章节分类接口
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#1 urls.py router.register('chapters', views.CourseChapterView, 'coursechapter') # 2 views.py class CourseChapterView(GenericViewSet,ListModelMixin): queryset = models.CourseChapter.objects.filter(is_delete=False,is_show=True) serializer_class = serializer.CourseChapterSerializer # 可以按照课程id来查 filter_backends = [DjangoFilterBackend] filter_fields = ['course'] # 3 serializer.py class CourseSectionSerializer(serializers.ModelSerializer): class Meta: model=models.CourseSection fields=['name','orders','duration','free_trail','section_link','section_type_name'] class CourseChapterSerializer(serializers.ModelSerializer): # 子序列化的方式 coursesections=CourseSectionSerializer(many=True) class Meta: model=models.CourseChapter fields=['name','summary','chapter','coursesections']View Code
标签:章节,name,fields,price,backends,接口,filter,过滤,class From: https://www.cnblogs.com/97zs/p/18022025