django中时间区间过滤

Yort 2020-02-22

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 ModelViewSetclass DoctorsViewSet(ModelViewSet):

     filter_backends = [DjangoFilterBackend]
      #过滤其他的字段
      filter_fields = (‘id‘, ‘patient‘, ‘doc_type‘,)        #指向要过滤的类
      filter_class = DoctorsFilter

相关推荐

inspuryhq / 0评论 2020-07-28