philipn/django-rest-framework-filters

Timestamp comparisons not working for date querystring values

nhaggmark opened this issue · 2 comments

Greetings!

I was just implementing a set of filters for a couple of timestamp fields in our system and I noticed that I could not get the gt, gte, lt, lte filters to work when plain dates are passed into the url.

Example:

class Meta:
    model = Foo
    fields = {
        'my_date': ('exact', 'gt', 'gte', 'lt', 'lte', ),
    }

If I use this querystring:

?my_date__gt=2016-11-24T08:41:17

I get the following whereclause added to my ORM query (Postgres):

"my_date" > '2016-11-24T08:41:17'::timestamp)

This is the expected behavior, however, if I use this querystring:

?my_date__gt=2016-11-24

I don't get any kind of error, it literally just doesn't execute a query to retrieve records at all. It returns an empty record set every time. If I then use a conventional definition of a django timestamp filter with the same querystring value:

import django_filters
my_date__gt = django_filters.DateTimeFilter(name="my_date", lookup_expr='gt')

I get the expected whereclause in my query of:

"my_date" > '2016-11-24T00:00:00'::timestamp)

In my experience, the typical behavior for filtering timestamp fields from date values is to implicitly convert the date to a timestamp. Am I doing something incorrectly?

I don't get any kind of error, it literally just doesn't execute a query to retrieve records at all.

By default, validation errors will return no results. This is most useful for stock Django views, where you're usually displaying the filter form with the result set. Obviously, not quite as useful in an API context, where there is no accompanying form. Instead, change the strictness option to raise a validation error.

Note that this has been changed in 2.0.0 (which is currently a pre-release), and the filter backend will automatically raise exceptions.

Going to close for now as I don't think see anything actionable, but feel free to continue asking questions if you need further help.