Skip to content

Wrong django-filter types #317

@markopy

Description

@markopy

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:

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingfix confirmation pendingissue has been fixed and confirmation from issue reporter is pending

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions