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

Get full result back from PeeWee query (for conversion to JSON)

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

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

这样做我只能从查询中返回一行。我很确定问题出在我对 get() 的使用上,文档清楚地说明只有 returns 一行。我用什么代替 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)})