本文介绍了django Rest Framework 按日期范围过滤的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有视图类

class eventList(ListAPIView):

       queryset = Event.objects.all().filter(is_active=1, is_approved=1)
       serializer_class = eventSerialiser
       filter_backends = [DjangoFilterBackend, filters.SearchFilter, filters.OrderingFilter]
       search_fields = ['event_name', 'event_address', 'start_date', 'start_time', 'end_time',
                         'age_max', 'age_min','event_organizer__name', 'event_type__name',
                         'event_city__name', 'event_tag__name']
       filterset_fields = ['event_name', 'start_date', 'start_time', 'end_date', 'end_time',
                           'age_max', 'age_min', 'event_organizer', 'event_type', 'event_city',
                           'event_tag']
       ordering_fields = '__all__'
       ordering = ['-id']

所有过滤器都工作正常,但我如何根据日期范围获取数据 start_dateend_date

All filters working fine but how i can get data against date rang start_date and end_date

推荐答案

好久了,终于得到这个解决方案

At last got this solution for long time

  • 双下划线gte表示大于等于
  • 双下划线gt表示大于
  • 双下划线 lte 表示小于等于
  • 双下划线lt表示小于

  • double underscore gte is for greater than equal to
  • double underscore gt is for greater than
  • double underscore lte is for less than equal to
  • double underscore lt is for less than

filterset_fields = {
    'start_date':['gte', 'lte', 'exact', 'gt', 'lt'],
    'id':['exact'],
    'event_name':['exact'],
    'start_time':['exact'],
    'end_date':['exact'],
    'end_time':['exact'],
    'age_max':['gte', 'lte', 'exact', 'gt', 'lt'],
    'age_min':['gte', 'lte', 'exact', 'gt', 'lt'],
    'event_organizer__name':['exact'],
    'event_type__name':['exact'],
    'event_city__name':['exact'],'event_tag__name':['exact']
}

这篇关于django Rest Framework 按日期范围过滤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 14:17