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

Extending global filter set overrides #1031

Open
an0o0nym opened this issue Aug 26, 2020 · 0 comments
Open

Extending global filter set overrides #1031

an0o0nym opened this issue Aug 26, 2020 · 0 comments

Comments

@an0o0nym
Copy link

Is your feature request related to a problem? Please describe.
I am generating DjangoObjectType from my models dynamically. Some of those models have custom model fields that I have defined in my project. This throws an error i.e.:

AssertionError: MyModelSet resolved field 'custom' with 'exact' lookup to an unrecognized field type CustomField. Try adding an override to 'Meta.filter_overrides'. See: https://django-filter.readthedocs.io/en/master/ref/filterset.html#customise-filter-generation-with-filter-overrides

Describe the solution you'd like
I would like to be able to extend global filterset overrides for those custom fields just like graphene-django does with GRAPHENE_FILTER_SET_OVERRIDES. Doing this by manually extending GrapheneFilterSetMixin.FILTER_DEFAULTS seems to work for my case.

I think this solution does not cause any backward incompatibility nor introduce any complexity. The solution is pretty easy to understand and introduce to the project code base. I hope this will add more flexibility to the project for future users facing similar problems.

class GrapheneFilterSetMixin(BaseFilterSet):
    """ A django_filters.filterset.BaseFilterSet with default filter overrides
    to handle global IDs """

    FILTER_DEFAULTS = dict(
        itertools.chain(
            FILTER_FOR_DBFIELD_DEFAULTS.items(), 
            GRAPHENE_FILTER_SET_OVERRIDES.items(), 
            getattr(settings, 'EXTRA_FILTER_SET_OVERRIDES', {}).items()
        )
    )

Describe alternatives you've considered
An alternative I have used before developing above solution was to quickly monkey patch solution, however this did not seem to work not to mention is not a long-time solution.

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

No branches or pull requests

1 participant