• django中时间区间过滤,排序


    1.过滤下载
    pip install django-filter
    
    2.下载后注册
    INSTALLED_APPS=['django_filters',]
    
    3.导入rest_framework
    from django_filters import rest_framework
    
    class DoctorsFilter(rest_framework.FilterSet):
        authentication_classes = [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 django_filters.rest_framework import DjangoFilterBackend
    from rest_framework.viewsets 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):
        
                queryset =models的类
                serializer_class=序列化组件
             filter_backends = [OrderingFilter]
            #/api/cars/?ordering=-id 倒序
       #/api/cars/?ordering=id 正序
    ordering_fields = ('id', )
  • 相关阅读:
    CG_Lession
    linux学习网站大全[转]
    C++ books
    Linux 建议学习路径[转]
    talking C++ STL
    Factory
    计算机图像图形学相关好书推荐
    ASP.NET控件缩写大全
    web开发面试题一
    ASP.Net面试题之二
  • 原文地址:https://www.cnblogs.com/xzcvblogs/p/12345054.html
Copyright © 2020-2023  润新知