Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django Rest Framework filter by date range

I have view class given

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']

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

like image 875
Sohail Ahmad Avatar asked Nov 13 '19 13:11

Sohail Ahmad


1 Answers

This work for me.

  • 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']
      }
    
like image 78
Sohail Ahmad Avatar answered Sep 21 '22 13:09

Sohail Ahmad