Python 有没有办法在peewee中查询.all()?

Python 有没有办法在peewee中查询.all()?,python,orm,sqlalchemy,peewee,Python,Orm,Sqlalchemy,Peewee,我需要将所有数据从SQL表传输到html页面。在SQLAlchemy中,我会这样做: 类Authordb.Model: id=db.Columndb.Integer,主键=True first=db.Columndb.String80 last=db.Columndb.String80 @应用程序路径“/作者” def get_作者: authors=Author.query.all 序列化查询集 结果=authors\u schema.dumpauthors 返回jsonify{'author

我需要将所有数据从SQL表传输到html页面。在SQLAlchemy中,我会这样做:

类Authordb.Model: id=db.Columndb.Integer,主键=True first=db.Columndb.String80 last=db.Columndb.String80 @应用程序路径“/作者” def get_作者: authors=Author.query.all 序列化查询集 结果=authors\u schema.dumpauthors 返回jsonify{'authors':result.data} peewee中是否有类似authors=Author.query.all的东西?

所以我做了这个

@app.route('/authors')
def get_authors():
    authors = Author.select()
    return render_template('aurhors.html', authors=authors)
在html中类似这样的东西

 {% for a in authors %}
 <p>{{a.author_name}}</p>
 {% endfor %}
我只是刚开始学习python,所以谢谢你的帮助。

所以我做了这个

@app.route('/authors')
def get_authors():
    authors = Author.select()
    return render_template('aurhors.html', authors=authors)
在html中类似这样的东西

 {% for a in authors %}
 <p>{{a.author_name}}</p>
 {% endfor %}

我只是刚开始学习python,所以谢谢你的帮助。

你要找的是.dicts,一种返回特定类型的iterable peewee的方法:

response = {'authors': []}
authors = Author.select()
for author in authors.dicts():
    response['authors'].append(author)

您要查找的是.dicts,这是一种返回iterable peewee特定类型的方法:

response = {'authors': []}
authors = Author.select()
for author in authors.dicts():
    response['authors'].append(author)

据我所知,peewee中不存在直接等价物,尽管在Dataset扩展中有一个all方法,已记录在案。使用列表理解,您可以非常轻松地完成此操作:

作者=[作者中作者的作者。选择] 甚至只是authors=listAuthor。但是,如果您试图将这些作为JSON返回,它将不起作用,因为您的作者列表由Author实例填充,而Flask的将无法使用这种开箱即用的类型。您可以使用peewee的dicts方法来解决此问题:

authors=[author.select.dicts中的author for author] 完整示例如下所示:

@应用程序路径“/作者” def get_作者: authors=[author.select.dicts中的author for author] 返回jsonifyauthors 我经常使用dict来代替序列化。例如,您创建了一个author\u模式,如下所示:

从棉花糖导入模式,字段 类作者Chemaschema: id=字段。仅Integerdump_=真 first=fields.String last=fields.String author\u schema=AuthorSchema 并像这样使用,不显示导入:

@应用程序路径“/作者” def get_作者: authors=author\u schemaAuthor,many=True 返回jsonifyauthors
据我所知,peewee中不存在直接等价物,尽管在Dataset扩展中有一个all方法,已记录在案。使用列表理解,您可以非常轻松地完成此操作:

作者=[作者中作者的作者。选择] 甚至只是authors=listAuthor。但是,如果您试图将这些作为JSON返回,它将不起作用,因为您的作者列表由Author实例填充,而Flask的将无法使用这种开箱即用的类型。您可以使用peewee的dicts方法来解决此问题:

authors=[author.select.dicts中的author for author] 完整示例如下所示:

@应用程序路径“/作者” def get_作者: authors=[author.select.dicts中的author for author] 返回jsonifyauthors 我经常使用dict来代替序列化。例如,您创建了一个author\u模式,如下所示:

从棉花糖导入模式,字段 类作者Chemaschema: id=字段。仅Integerdump_=真 first=fields.String last=fields.String author\u schema=AuthorSchema 并像这样使用,不显示导入:

@应用程序路径“/作者” def get_作者: authors=author\u schemaAuthor,many=True 返回jsonifyauthors
尊敬的downvoter,如果上述内容有任何问题,请您发表意见?代码经过测试,我的答案比其他人更完整。亲爱的downvoter,如果上面有任何错误,请您发表意见?代码经过测试,我的答案比其他人更完整。。