我设置了一个 flask_restful API,但希望用户在进行 post 调用时传递 numpy 数组。我第一次尝试使用 reqparse
来自 flask_restful 但它不支持 numpy.array 的数据类型。我正在尝试使用棉花糖(flask_marshmallow),但无法理解参数是如何传递给 API 的。
在我能够做类似的事情之前:
from flask_restful import reqparse
parser = reqparse.RequestParser()
parser.add_argument('url', type=str)
parser.add_argument('id', type=str, required=True)
但现在我不知道如何把它翻译成棉花糖。我有这个设置:
from flask_marshmallow import Marshmallow
app = Flask(__name__)
ma = Marshmallow(app)
class MySchema(ma.Schema):
id = fields.String(required=True)
url = fields.String()
但是,如何在使用我新定义的模式进行调用时加载用户传入的内容?
最佳答案
我不知道flask-marshmallow,文档只展示了如何使用它来序列化响应。
我想反序列化请求,你需要使用 webargs ,由棉花糖团队开发和维护。
它有 marshmallow integration :
from marshmallow import Schema, fields
from webargs.flaskparser import use_args
class UserSchema(Schema):
id = fields.Int(dump_only=True) # read-only (won't be parsed by webargs)
username = fields.Str(required=True)
password = fields.Str(load_only=True) # write-only
first_name = fields.Str(missing="")
last_name = fields.Str(missing="")
date_registered = fields.DateTime(dump_only=True)
@use_kwargs(UserSchema())
def profile_update(username, password, first_name, last_name):
update_profile(username, password, first_name, last_name)
# ...
关于python - 如何使用 marshmallow 和 flask_restful 加载参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55582994/