-
Notifications
You must be signed in to change notification settings - Fork 306
Description
django-filter fields can have a different type from the underlying model field they operate against. drf-spectacular incorrectly makes the schema type match the model field instead of the filter.
Take the following filter for example:
class UserFilter(django_filters.FilterSet):
has_logged_in = django_filters.BooleanFilter(field_name='last_login', lookup_expr='isnull')
last_login is a datetime model field but the filter field is boolean. Currently the schema has the type for has_logged_in as datetime which is incorrect.
The reason, I believe, is this part of the code:
drf-spectacular/drf_spectacular/contrib/django_filters.py
Lines 57 to 60 in 243445f
| if isinstance(model_field, models.Field): | |
| schema = auto_schema._map_model_field(model_field, direction=None) | |
| else: | |
| schema = self.map_filter_field(filter_field) |
The model field type is preferred over the actual filter type.
I don't understand why the model field is even used here. Is it to get additional information? Since the filter itself has a clear type I don't think the mode field type is ever relevant.