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

Filter.method not support negation/exclusion #319

Closed
gustabot42 opened this issue Oct 31, 2019 · 1 comment
Closed

Filter.method not support negation/exclusion #319

gustabot42 opened this issue Oct 31, 2019 · 1 comment

Comments

@gustabot42
Copy link

gustabot42 commented Oct 31, 2019

The actual signature of filter method does not permit to resolve automatic filter negations !=.
Automatic filter negation depends on the exclude attribute but this attribute is not accessible with the actual filter method signature.
A possible solution is to change the signature from (qs, name, value) to (qs, name, value, exclude)

class FilterMethod(object):
    def __call__(self, qs, value):
        if value in EMPTY_VALUES:
            return qs
        return self.method(qs, self.f.field_name, value, self.f.exclude)
@gustabot42 gustabot42 changed the title Filter.method do not support negation/exclusion Filter.method not support negation/exclusion Oct 31, 2019
@rpkilby
Copy link
Collaborator

rpkilby commented Nov 15, 2019

Hi @gustabot42. Good catch. I've opened carltongibson/django-filter#1150 to track this.

As a workaround, you could use partialmethod to bind the filter attribute name to the method, then lookup the filter and its attributes from there. e.g.,

class MyFilter(FilterSet):
    date = IsoDateFilter(field_name='f', lookup_expr='exact', method='date_filter')
    not_date = IsoDateFilter(field_name='f', lookup_expr='exact', exclude=True, method='not_date_filter')

    def generic_date_filter(self, qs, field_name, value, attr):
        f = self.filters[attr]
        # do something with `f.exclude`
    
    date_filter = partialmethod(generic_date_filter, attr='date')
    not_date_filter = partialmethod(generic_date_filter, attr='not_date')

That said, closing as this is an issue with django-filter and not DRFF.

@rpkilby rpkilby closed this as completed Nov 15, 2019
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

2 participants