django FilterSet为一天的结束

时间:2018-05-25 09:09:25

标签: django datefilter

如何使查询从FilterSet的开始日期和结束日期开始采用正确的日期。 现在,如果键入start_filter_date = 2018.05.23和end_filter_date = 2018.05.25。然后start_filter_date = 2018.05.23T00:00和end_filter_date = 2018.05.25T00:00。时间是00:00,但需要23:59?

我的课程如下:

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")

1 个答案:

答案 0 :(得分:4)

我认为最好的方法是使用gte lt,但发送结束日期date_added + timedelta(1),我不会使用django过滤器,但会基于tips.html#solution-1-magic-values你可以尝试:

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")