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

Enhancement: A simple way to add pseudo-lookups for FilterSet's Meta.fields #1575

Open
WhiteSage opened this issue May 1, 2023 · 0 comments

Comments

@WhiteSage
Copy link

WhiteSage commented May 1, 2023

One of the proposed solutions in the documentation for filtering for empty strings is adding the lookup __isempty. This can be done by registering it at the Django level (and fiddling directly with the SQL compiler) or by adding a filter which responds to the specified lookup.

Automatic generation through FilterSet.Meta.fields however fails, even if we overwrite the FilterSet.filter_for_lookup() function:

    @classmethod
    def filter_for_lookup(cls, field, lookup_type):

        if lookup_type == "isempty":
            return EmptyStringFilter, {}
        
        return super().filter_for_lookup(field, lookup_type)

The reason is FilterSet.filter_for_field() calls the resolve_field() function on the utils module, which then uses the Django machinery to determine which part are transformers and which part is lookup, and what field type results after applying the transforms. There is no way to override the resolve_field() call without overriding the whole FilterSet.filter_for_field() function.

It would be nice if some support could be added for custom lookups. This seems a complex issue, but at least the resolve_field() call could get wrapped into a FilterSet class function which can be easily overriden in case we have to support custom lookups.

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

1 participant