gpt4 book ai didi

python - 从 PeeWee 查询中获取完整结果(用于转换为 JSON)

转载 作者:行者123 更新时间:2023-11-28 19:54:39 30 4
gpt4 key购买 nike

我正在尝试使用以下代码将 PeeWee 查询结果呈现为 JSON:

@app.route('/')
def index():
c = Category.select().order_by(Category.name).get()
return jsonify(model_to_dict(c))

这样做我只能从查询中返回一行。我很确定问题出在我对 get() 的使用上,文档明确表示它只返回一行。我用什么代替 get() 来取回整个结果?

下面的这个问题为我指明了正确的方向,但它也在使用 get()

Peewee model to JSON

最佳答案

What do I use in place of get() to fetch the entire results back?

将您的代码修改为:

query = Category.select().order_by(Category.name)
return jsonify({'rows':[model_to_dict(c) for c in query]})

或者,您可以:

query = Category.select().order_by(Category.name).dicts()
return jsonify({'rows':list(query)})

关于python - 从 PeeWee 查询中获取完整结果(用于转换为 JSON),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35225682/

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