Search code examples
djangodjango-rest-frameworkdjango-filter

GET 'today' data in Django-Filter/DRF


I'm trying to get the today or yesterday data from the current day using django-filter/DRF.

class SampleFilter(filters.FilterSet):
    start_date = DateTimeFilter(field_name='timestamp', lookup_expr='gte')
    end_date = DateTimeFilter(field_name='timestamp', lookup_expr='lte')

    class Meta:
        model = Sample
        fields = []

on my django-rest api url. when I get the data start_date=${start}&end_date=${end} for the dates of today, it returns zero data even it have in the database.

"GET /api/v1/sample/?start_date=2021-02-02&end_date=2021-02-02 HTTP/1.1" 200 2" it is 200 success but it returns nothing.

What's wrong in my code? please help. thanks!


Solution

  • You are using a DateTimeFilter, not a DateFilter.

    By supplying a value like "2021-02-02" - i.e. a date, not a datetime - it's failing to parse, and silently discarding the value and the filter.

    Try either changing to a DateFilter, or passing a full datetime as the argument.