djangodatefilter

django FilterSet for end of day


How to make the query take correct date from start date and end end of the day for FilterSet. Now if you type start_filter_date = 2018.05.23 and end_filter_date = 2018.05.25. Then start_filter_date=2018.05.23T00:00 and end_filter_date=2018.05.25T00:00. Time is taken 00:00, but need to be 23:59?

My class is following:

class TaskFilterSet(django_filters.rest_framework.FilterSet):
    id = django_filters.NumberFilter(name="pk")
    start_filter_date = django_filters.DateFilter(name="date_added", lookup_expr="gte")
    end_filter_date = django_filters.DateFilter(name="date_added", lookup_expr="lte")

Solution

  • i think the best way is use gte with lt but send end date date_added + timedelta(1), i don't use the django filters but based on the tips.html#solution-1-magic-values you can try:

    class EndFilter(django_filters.DateFilter):
    
        def filter(self, qs, value):
            if value:
                value = value + timdelta(1)
            return super(EndFilter, self).filter(qs, value)
    
    
    class TaskFilterSet(django_filters.rest_framework.FilterSet):
    
        id = django_filters.NumberFilter(name="pk")
        start_filter_date = django_filters.DateFilter(name="date_added", lookup_expr="gte")
        end_filter_date = EndFilter(name="date_added", lookup_expr="lt")