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