0

我正在尝试图像上传 API 我收到以下错误

127.0.0.1  "POST //upload/image HTTP/1.1" 500 -
Traceback (most recent call last):
  File "D:\c_code\projects\python_api\.venv\lib\site-packages\flask\app.py", line 2463, in __call__
    return self.wsgi_app(environ, start_response)
  File "D:\c_code\projects\python_api\.venv\lib\site-packages\flask\app.py", line 2449, in wsgi_app
    response = self.handle_exception(e)
  File "D:\c_code\projects\python_api\.venv\lib\site-packages\flask_cors\extension.py", line 161, in wrapped_function
    return cors_after_request(app.make_response(f(*args, **kwargs)))
  File "D:\c_code\projects\python_api\.venv\lib\site-packages\flask_restful\__init__.py", line 272, in error_router
    return original_handler(e)
  File "D:\c_code\projects\python_api\.venv\lib\site-packages\flask\app.py", line 1866, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "D:\c_code\projects\python_api\.venv\lib\site-packages\flask\_compat.py", line 38, in reraise
    raise value.with_traceback(tb)
  File "D:\c_code\projects\python_api\.venv\lib\site-packages\flask\app.py", line 2446, in wsgi_app
    response = self.full_dispatch_request()
  File "D:\c_code\projects\python_api\.venv\lib\site-packages\flask\app.py", line 1951, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "D:\c_code\projects\python_api\.venv\lib\site-packages\flask_cors\extension.py", line 161, in wrapped_function
    return cors_after_request(app.make_response(f(*args, **kwargs)))
  File "D:\c_code\projects\python_api\.venv\lib\site-packages\flask_restful\__init__.py", line 272, in error_router
    return original_handler(e)
  File "D:\c_code\projects\python_api\.venv\lib\site-packages\flask\app.py", line 1820, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "D:\c_code\projects\python_api\.venv\lib\site-packages\flask\_compat.py", line 38, in reraise
    raise value.with_traceback(tb)
  File "D:\c_code\projects\python_api\.venv\lib\site-packages\flask\app.py", line 1949, in full_dispatch_request
    rv = self.dispatch_request()
  File "D:\c_code\projects\python_api\.venv\lib\site-packages\flask\app.py", line 1935, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "D:\c_code\projects\python_api\.venv\lib\site-packages\flask_restful\__init__.py", line 468, in wrapper
    resp = resource(*args, **kwargs)
  File "D:\c_code\projects\python_api\.venv\lib\site-packages\flask\views.py", line 89, in view
    return self.dispatch_request(*args, **kwargs)
  File "D:\c_code\projects\python_api\.venv\lib\site-packages\flask_restful\__init__.py", line 583, in dispatch_request
    resp = meth(*args, **kwargs)
  File "D:\c_code\projects\python_api\.venv\lib\site-packages\flask_jwt_extended\view_decorators.py", line 108, in wrapper
    return fn(*args, **kwargs)
  File "D:\c_code\projects\python_api\resources\image.py", line 24, in post
    data = image_schema.load(request.files)
  File "D:\c_code\projects\python_api\.venv\lib\site-packages\marshmallow\schema.py", line 723, in load
    data, many=many,  unknown=unknown, postprocess=True
  File "D:\c_code\projects\python_api\.venv\lib\site-packages\marshmallow\schema.py", line 861, in _do_load
    unknown=unknown,
  File "D:\c_code\projects\python_api\.venv\lib\site-packages\marshmallow\schema.py", line 669, in _deserialize
    index=index,
  File "D:\c_code\projects\python_api\.venv\lib\site-packages\marshmallow\schema.py", line 493, in _call_and_store
    value = getter_func(data)
  File "D:\c_code\projects\python_api\.venv\lib\site-packages\marshmallow\schema.py", line 662, in <lambda>
    val, field_name, data, **d_kwargs
  File "D:\c_code\projects\python_api\.venv\lib\site-packages\marshmallow\fields.py", line 342, in deserialize
    output = self._deserialize(value, attr, data, **kwargs)
TypeError: _deserialize() got an unexpected keyword argument 'partial'

的代码File "D:\c_code\projects\python_api\resources\image.py", line 24, in post如下

from marshmallow import Schema, fields
from werkzeug.datastructures import FileStorage

from marshmallow import Schema, fields
from werkzeug.datastructures import FileStorage


class FileStorageField(fields.Field):
    default_error_messages = {
        "invalid": "Not a valid image."
    }

    def _deserialize(self, value, attr, data) -> FileStorage:
        if value is None:
            return None

        if not isinstance(value, FileStorage):
            self.fail("invalid")

        return value


class ImageSchema(Schema):
    image = FileStorageField(required=True)

调用 API 图像上传时代码工作正常,但会引发错误。我正在使用烧瓶棉花糖,烧瓶上传,包。

4

1 回答 1

2

由于棉花糖 3,_deserialize可能会收到partialkwarg。

每个自定义字段都应该复制Field._deserialize签名并接受未知的 kwargs。

    def _deserialize(
        self,
        value: typing.Any,
        attr: typing.Optional[str],
        data: typing.Optional[typing.Mapping[str, typing.Any]],
        **kwargs
    ):

像这样更改您的代码以静默接受 kwargs:

    def _deserialize(self, value, attr, data, **kwargs) -> FileStorage:
        if value is None:
            return None
于 2020-03-21T15:17:47.373 回答