django-filter实现区间过滤

# filters.py

from django_filters import FilterSet

class CourseFilterSet(FilterSet):
    
    # 课程的价格范围要大于等于min_price,小于等于max_price
    min_price = filters.NumberFilter(field_name='price', lookup_expr='gte')
    max_price = filters.NumberFilter(field_name='price', lookup_expr='lte')
    
    class Meta:
        model=models.Course
        fields=['course_category']
        
# views.py

class TestView(GenericViewSet, ListModelMixin):
     filter_backends=[DjangoFilterBackend]
	 # 配置类:(自己写的类)
     filter_class = CourseFilterSet

posted @ 2020-07-28 17:24  the3times  阅读(652)  评论(0编辑  收藏  举报