django-rest-framework-json-api icon indicating copy to clipboard operation
django-rest-framework-json-api copied to clipboard

Filter validation too rigid for filters with suffixes

Open SafaAlfulaij opened this issue 4 years ago • 5 comments

Here: https://github.com/django-json-api/django-rest-framework-json-api/blob/fbb4b033a483b7edb62de3377c83a046a55f69b2/rest_framework_json_api/django_filters/backends.py#L80

RangeFilter is used to specify a value of the two ends without specifying the other. It works by looking for "min/after" and "max/before" in the query param.

This should work:

Using DateFromToRangeFilter, defined this way:

last_updated = DateFromToRangeFilter()

and passing the filter: filters[last_updated_after]=2016-01-01

will execute: .filter(last_updated__gte="2016-01-01")

Currently it fails as last_updated_after != last_updated (as in the quoted code: base_filters contains last_updated but not last_updated_after or last_updated_before)

SafaAlfulaij avatar May 02 '21 20:05 SafaAlfulaij

I agree this is a bug and this should actually work. There are also other suffixes used like min and max which should work too. Any there could be custom suffixes as well. So I wonder instead of validating those filters on our own, whether there is a easy way to let the filter class validate the filters.

sliverc avatar May 21 '21 16:05 sliverc

Yes, it looks like it's a duplicate, sorry for opining another issue.

Are there any plans to fix it? This issue has been open for more than a year.

eloi-martinez-qida avatar Sep 12 '22 18:09 eloi-martinez-qida

@eloi-martinez-qida No worries. Welcome to DJA! This is an OSS project with only volunteer contributors. So people work on the issues which are most relevant to them.

Therefore, if you are affected by this issue, I encourage you to work on a PR. Happy to assist if needed.

sliverc avatar Sep 12 '22 19:09 sliverc

@sliverc What would be the best way to tackle the problem?

eloi-martinez-qida avatar Sep 13 '22 08:09 eloi-martinez-qida

The best way would be to check whether the django filter class has a way to validate the filter names.

sliverc avatar Sep 13 '22 13:09 sliverc