# Buggy code
def get_schema_operation_parameters(self, auto_schema, *args, **kwargs):
    if issubclass(self.target_class, SpectacularDjangoFilterBackendMixin):
        warn(
            "DEPRECATED - Spectacular's DjangoFilterBackend is superseded by extension. you "
            "can simply restore this to the original class, extensions will take care of the "
            "rest."
        )

    model = get_view_model(auto_schema.view)
    if not model:
        return []

    filterset_class = self.target.get_filterset_class(
        auto_schema.view, model.objects.none()
    )
    if not filterset_class:
        return []

    parameters = []
    for field_name, field in filterset_class.base_filters.items():
        path = field.field_name.split("__")
        model_field = follow_field_source(model, path)

        parameters.append(
            build_parameter_type(
                name=field_name,
                required=field.extra["required"],
                location=OpenApiParameter.QUERY,
                description=field.label if field.label is not None else field_name,
                schema=auto_schema._map_model_field(model_field, direction=None),
                enum=[c for c, _ in field.extra.get("choices", [])],
            )
        )

    return parameters


# Traceback (AssertionError)
Environment:


Request Method: GET
Request URL: http://localhost:8000/api/schema/

Django Version: 3.1.5
Python Version: 3.8.2
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.sites',
'corsheaders',
'rest_framework',
'rest_framework.authtoken',
'taggit',
'taggit_serializer',
'allauth',
'allauth.account',
'allauth.socialaccount',
'allauth.socialaccount.providers.google',
'rest_auth',
'rest_auth.registration',
'django_filters',
'private_storage',
'rest_mediabrowser',
'drf_spectacular',
'users',
'content',
'enrollment',
'forum',
'logs',
'billing']
Installed Middleware:
['corsheaders.middleware.CorsMiddleware',
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']



Traceback (most recent call last):
File "/root/.local/share/virtualenvs/server-MrgTHRtU/lib/python3.8/site-packages/django/core/handlers/exception.py", line 47, in inner
response = get_response(request)
File "/root/.local/share/virtualenvs/server-MrgTHRtU/lib/python3.8/site-packages/django/core/handlers/base.py", line 181, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/root/.local/share/virtualenvs/server-MrgTHRtU/lib/python3.8/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
return view_func(*args, **kwargs)
File "/root/.local/share/virtualenvs/server-MrgTHRtU/lib/python3.8/site-packages/django/views/generic/base.py", line 70, in view
return self.dispatch(request, *args, **kwargs)
File "/root/.local/share/virtualenvs/server-MrgTHRtU/lib/python3.8/site-packages/rest_framework/views.py", line 509, in dispatch
response = self.handle_exception(exc)
File "/root/.local/share/virtualenvs/server-MrgTHRtU/lib/python3.8/site-packages/rest_framework/views.py", line 469, in handle_exception
self.raise_uncaught_exception(exc)
File "/root/.local/share/virtualenvs/server-MrgTHRtU/lib/python3.8/site-packages/rest_framework/views.py", line 480, in raise_uncaught_exception
raise exc
File "/root/.local/share/virtualenvs/server-MrgTHRtU/lib/python3.8/site-packages/rest_framework/views.py", line 506, in dispatch
response = handler(request, *args, **kwargs)
File "/root/.local/share/virtualenvs/server-MrgTHRtU/lib/python3.8/site-packages/drf_spectacular/views.py", line 61, in get
return self._get_schema_response(request)
File "/root/.local/share/virtualenvs/server-MrgTHRtU/lib/python3.8/site-packages/drf_spectacular/views.py", line 65, in _get_schema_response
return Response(generator.get_schema(request=request, public=self.serve_public))
File "/root/.local/share/virtualenvs/server-MrgTHRtU/lib/python3.8/site-packages/drf_spectacular/generators.py", line 188, in get_schema
paths=self.parse(request, public),
File "/root/.local/share/virtualenvs/server-MrgTHRtU/lib/python3.8/site-packages/drf_spectacular/generators.py", line 165, in parse
operation = view.schema.get_operation(path, path_regex, method, self.registry)
File "/root/.local/share/virtualenvs/server-MrgTHRtU/lib/python3.8/site-packages/drf_spectacular/openapi.py", line 62, in get_operation
parameters = self._get_parameters()
File "/root/.local/share/virtualenvs/server-MrgTHRtU/lib/python3.8/site-packages/drf_spectacular/openapi.py", line 186, in _get_parameters
**dict_helper(self._get_filter_parameters()),
File "/root/.local/share/virtualenvs/server-MrgTHRtU/lib/python3.8/site-packages/drf_spectacular/openapi.py", line 353, in _get_filter_parameters
parameters += filter_extension.get_schema_operation_parameters(self)
File "/root/.local/share/virtualenvs/server-MrgTHRtU/lib/python3.8/site-packages/drf_spectacular/contrib/django_filters.py", line 42, in get_schema_operation_parameters
schema=auto_schema._map_model_field(model_field, direction=None),
File "/root/.local/share/virtualenvs/server-MrgTHRtU/lib/python3.8/site-packages/drf_spectacular/openapi.py", line 373, in _map_model_field
assert isinstance(model_field, models.Field)

Exception Type: AssertionError at /api/schema/
Exception Value:

# Fixed code
def get_schema_operation_parameters(self, auto_schema, *args, **kwargs):
    if issubclass(self.target_class, SpectacularDjangoFilterBackendMixin):
        warn(
            "DEPRECATED - Spectacular's DjangoFilterBackend is superseded by extension. you "
            "can simply restore this to the original class, extensions will take care of the "
            "rest."
        )

    model = get_view_model(auto_schema.view)
    if not model:
        return []

    filterset_class = self.target.get_filterset_class(
        auto_schema.view, model.objects.none()
    )
    if not filterset_class:
        return []

    parameters = []
    for field_name, field in filterset_class.base_filters.items():
        parameters.append(
            build_parameter_type(
                name=field_name,
                required=field.extra["required"],
                location=OpenApiParameter.QUERY,
                description=field.label if field.label is not None else field_name,
                schema=self.resolve_filter_field(
                    auto_schema, model, filterset_class, field
                ),
                enum=[c for c, _ in field.extra.get("choices", [])],
            )
        )

    return parameters