gpt4 book ai didi

json - 类型错误 : Object of type is not JSON serializable

转载 作者:行者123 更新时间:2023-12-03 16:13:14 26 4
gpt4 key购买 nike

我写了 rest在 Flask 上使用 flask-marshmallow
模型.py

class Application(db.Model):
__tablename__ = 'applications'

id = db.Column(db.String(), primary_key=True)
name = db.Column(db.String())
versions = db.relationship('Version', backref='application', lazy=True)

def __repr__(self):
return '<application {}>'.format(self.name)


class Version(db.Model):
__tablename__ = 'versions'

id = db.Column(db.String(), primary_key=True)
file = db.Column(db.String(80), nullable=True)
application_id = db.Column(db.Integer, db.ForeignKey('applications.id'))

shemas.py
class ApplicationDetailSchema(ma.Schema):
class Meta:
fields = ('id', 'name', 'versions')

路线.py
@bp.route("/<id>")
def application_detail(id):
application = Application.query.get(id)
result = application_detail_schema.dump(application)
return jsonify(result)

TypeError: Object of type 'Version' is not JSON serializable

最佳答案

您可能想使用 ModelSchema而不是 Schema .

class ApplicationDetailSchema(ma.ModelSchema):
class Meta:
model = Application
fields = ('id', 'name', 'versions')
ModelSchema默认情况下,将相关外键对象转储为 id(s) 列表,这是 JSON 可序列化的。

关于json - 类型错误 : Object of type is not JSON serializable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58343453/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com