django中时间区间过滤,排序1.过滤下载
pip install django-filter
2.下载后注册
INSTALLED_APPS=['django_filters',]凝聚作文600字
3.导⼊rest_framework喝牛奶养胃吗
from django_filters import rest_framework
class DoctorsFilter(rest_framework.FilterSet):
authentication_class = [TokenAuthentication]
#开始时间
start_date = rest_framework.DateFilter(field_name='start_date', lookup_expr='gte')
#结束时间
end_date = rest_framework.DateFilter(field_name='end_date', lookup_expr='lte')产品购销合同
4.在类中的使⽤
from st_framework import DjangoFilterBackend
肩周炎偏方from rest_framework.viewts import ModelViewSet珍惜所拥有的
class DoctorsViewSet(ModelViewSet):臀窝
filter_backends = [DjangoFilterBackend]
描写芦苇的诗句
#过滤其他的字段
filter_fields = ('id', 'patient', 'doc_type',)
#指向要过滤的类
filter_class = DoctorsFilter
铭记历史的名言2.排序
#1.导⼊OrderingFilter
from rest_framework.filters import OrderingFilter
#2.在类中的使⽤
class CarsModelViewSet(ModelViewSet):
queryt =models的类
rializer_class=序列化组件
filter_backends = [OrderingFilter]
#/api/cars/?ordering=-id 倒序
#/api/cars/?ordering=id 正序
ordering_fields = ('id', )