graphql-python / graphql-python/graphene-django
Extending global filter set overrides
- Dominant language
- Python
- Stars
- 4.4k
- Forks
- 760
- PR merge metrics
- No merged PRs in 30d
Description
**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.:
```python
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.
```python
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.
Contributor guide
Assessment
This issue has not been assessed yet.