Django FileField导致的Swagger异常

年前接的能源项目弄到现在还没交付出去,甲方的需求一直在变,另外对于能源行业也了解的并没有那么深入,就导致项目做起来非常累。最近几年其实没怎么写过代码,除了一些需要设计或者对接算法以及处理文本图像等代码其他人员写不了,会写个接口来出来一下。这个能源项目干到现在相当于把过去几年没写的代码都补回来了。

今天早上更新了下数据model结果文档又出错了:

Unable to render this definition
The provided definition does not specify a valid version field.

Please indicate a valid Swagger or OpenAPI version field. Supported version fields are swagger: "2.0" and those that match openapi: 3.0.n (for example, openapi: 3.0.0).

这个问题前端时间其实遇到过一次了,不过当时参考了一下其他的FileField的设置,解决了,结果今天出这个问题忘了怎么解决的了,又查找半天。网上随便一搜,都是废话,解决不了问题。

那还是直接看错误日志吧:

Django version 3.2.3, using settings 'application.settings'
Starting development server at http://0.0.0.0:8000/
Quit the server with CONTROL-C.
[Errno 48] Address already in use
!!!scheduler already started, DO NOTHING
[2023-03-04 10:45:41][django.server.log_message():161] [INFO] "GET / HTTP/1.1" 200 2504
[2023-03-04 10:45:42][dvadmin.utils.exception.CustomExceptionHandler():46] [ERROR] Traceback (most recent call last):
  File "/Users/obaby/PycharmProjects/django-vue-admin/backend/venv/lib/python3.8/site-packages/rest_framework/views.py", line 506, in dispatch
    response = handler(request, *args, **kwargs)
  File "/Users/obaby/PycharmProjects/django-vue-admin/backend/venv/lib/python3.8/site-packages/drf_yasg/views.py", line 94, in get
    schema = generator.get_schema(request, self.public)
  File "/Users/obaby/PycharmProjects/django-vue-admin/backend/dvadmin/utils/swagger.py", line 39, in get_schema
    swagger = super().get_schema(request, public)
  File "/Users/obaby/PycharmProjects/django-vue-admin/backend/venv/lib/python3.8/site-packages/drf_yasg/generators.py", line 246, in get_schema
    paths, prefix = self.get_paths(endpoints, components, request, public)
  File "/Users/obaby/PycharmProjects/django-vue-admin/backend/venv/lib/python3.8/site-packages/drf_yasg/generators.py", line 404, in get_paths
    operation = self.get_operation(view, path, prefix, method, components, request)
  File "/Users/obaby/PycharmProjects/django-vue-admin/backend/venv/lib/python3.8/site-packages/drf_yasg/generators.py", line 446, in get_operation
    operation = view_inspector.get_operation(operation_keys)
  File "/Users/obaby/PycharmProjects/django-vue-admin/backend/venv/lib/python3.8/site-packages/drf_yasg/inspectors/view.py", line 33, in get_operation
    query = self.get_query_parameters()
  File "/Users/obaby/PycharmProjects/django-vue-admin/backend/venv/lib/python3.8/site-packages/drf_yasg/inspectors/view.py", line 291, in get_query_parameters
    natural_parameters = self.get_filter_parameters() + self.get_pagination_parameters()
  File "/Users/obaby/PycharmProjects/django-vue-admin/backend/venv/lib/python3.8/site-packages/drf_yasg/inspectors/base.py", line 408, in get_filter_parameters
    fields += self.probe_inspectors(self.filter_inspectors, 'get_filter_parameters', filter_backend()) or []
  File "/Users/obaby/PycharmProjects/django-vue-admin/backend/venv/lib/python3.8/site-packages/drf_yasg/inspectors/base.py", line 110, in probe_inspectors
    result = method(obj, **kwargs)
  File "/Users/obaby/PycharmProjects/django-vue-admin/backend/venv/lib/python3.8/site-packages/drf_yasg/inspectors/query.py", line 26, in get_filter_parameters
    fields = filter_backend.get_schema_fields(self.view)
  File "/Users/obaby/PycharmProjects/django-vue-admin/backend/venv/lib/python3.8/site-packages/django_filters/rest_framework/backends.py", line 131, in get_schema_fields
    filterset_class = self.get_filterset_class(view, queryset)
  File "/Users/obaby/PycharmProjects/django-vue-admin/backend/dvadmin/utils/filters.py", line 214, in get_filterset_class
    class AutoFilterSet(self.filterset_base):
  File "/Users/obaby/PycharmProjects/django-vue-admin/backend/venv/lib/python3.8/site-packages/django_filters/filterset.py", line 71, in __new__
    new_class.base_filters = new_class.get_filters()
  File "/Users/obaby/PycharmProjects/django-vue-admin/backend/dvadmin/utils/filters.py", line 252, in get_filters
    filters[filter_name] = cls.filter_for_field(
  File "/Users/obaby/PycharmProjects/django-vue-admin/backend/venv/lib/python3.8/site-packages/django_filters/filterset.py", line 382, in filter_for_field
    assert filter_class is not None, (
AssertionError: AutoFilterSet resolved field 'code_file' with 'exact' lookup to an unrecognized field type FileField. Try adding an override to 'Meta.filter_overrides'. See: https://django-filter.readthedocs.io/en/main/ref/filterset.html#customise-filter-generation-with-filter-overridesg

其实关键就在最后一行:AutoFilterSet resolved field ‘code_file’ with ‘exact’ lookup to an unrecognized field type FileField,新建的code_file无法通过自动筛选器的筛选,所以要解决这个问题只需要将查询对象中把文件路径去掉就ok了,在class中添加以下代码:

class QRcodeViewSet(CustomModelViewSet):
    """
    设备二维码管理接口
    list:查询
    create:新增
    update:修改
    retrieve:单例
    destroy:删除
    """
    queryset = QRcode.objects.all()
    serializer_class = QRcodeSerializer
    extra_filter_backends = []
    filter_fields = ['qr_code', 'substation_device', ] # 筛选条件排除掉文件FileFields即可

重启服务就ok了:

出了问题还是老老实实看日志吧,想着偷懒搜索一下解决,那很可能也解决不了。

☆版权☆

* 网站名称:obaby@mars
* 网址:https://oba.by/
* 个性:https://oba.by/
* 本文标题: 《Django FileField导致的Swagger异常》
* 本文链接:https://lang.bi/2023/03/11388
* 短链接:https://oba.by/?p=11388
* 转载文章请标明文章来源,原文标题以及原文链接。请遵从 《署名-非商业性使用-相同方式共享 2.5 中国大陆 (CC BY-NC-SA 2.5 CN) 》许可协议。


You may also like

3 comments

    1. 公主 Queen 
      Google Chrome 102 Google Chrome 102 Mac OS X 10.15 Mac OS X 10.15 cn山东省济南市 移动

      竖版的就会用两张,嘿嘿,为了占满板面。

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注