Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IsoDateTimeFilter problems comparison other than 'exact' #780

Closed
gsvr opened this issue Sep 14, 2017 · 3 comments
Closed

IsoDateTimeFilter problems comparison other than 'exact' #780

gsvr opened this issue Sep 14, 2017 · 3 comments

Comments

@gsvr
Copy link

gsvr commented Sep 14, 2017

I've been trying to implement the filtering described here: http://django-filter.readthedocs.io/en/develop/ref/filters.html#isodatetimefilter

While that method works, I wanted more than just 'exact' matching, so I replaced:
fields = ['published']
with:
fields = {'published': ['exact', 'gte', 'lte']}
This still works for 'exact' filtering with an IsoDateTime, but for 'gte' and 'lte', it fails when using an IsoDateTime. It works when using a DateTime.

I am using a workaround at the moment which does work with IsoDateTime by specifying each lookup seperately:

published = django_filters.IsoDateTimeFilter(
    name='published', lookup_expr='exact')
published_gte = django_filters.IsoDateTimeFilter(
    name='published', lookup_expr='gte')
published_lte = django_filters.IsoDateTimeFilter(
    name='published', lookup_expr='lte')
@carltongibson
Copy link
Owner

Hmmm. My first response it to just accept this as a limitation of the dictionary fields syntax. Declaring fields explicitly — your "workaround" — is the canonical way to do it. The dictionary syntax is just a shortcut, and there's no problem with it having limitations.

Having said that, I'd take a change with a small footprint to "fix" this...

@rpkilby
Copy link
Collaborator

rpkilby commented Sep 14, 2017

You can use Meta.filter_overrides to change the filter class used for a model field.

class BookFilter(django_filters.FilterSet):

    class Meta:
        model = Book
        fields = {'published': ['exact', 'lte', 'gte']}
        filter_overrides = {
            models.DateTimeField: {'filter_class': django_filters.IsoDateTimeFilter},
        }

@gsvr
Copy link
Author

gsvr commented Sep 14, 2017

@carltongibson I'm happy with accepting the limitation. I tried looking for a quick "fix" but my understanding of the inner workings of the code is too limited at the moment :/

@rpkilby Thanks, your suggestion works well, I'll use that going forward :)

@carltongibson Would you like to note this limitation in the docs? I'm happy to make a pull request for it if you think it's appropriate.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants