将 Firestore 与 Flask/Jinja 一起使用,这是查询结果并将其显示到 DOM 的正确方法


@app.route('/view_case/<case_name>')
def view_case(case_name):
    query = db.collection('cases').document(case_name).collection('documents').get()
    documents = []
    for _document in query:
        documents.append(_document)
    return render_template('views/view_case.html', documents=documents)

上述方法是否是查询一组文档并将它们作为列表发送到 DOM 的正确方法,以供 jinja 迭代显示?

附带问题,我注意到结果不包括文档 ID,有没有办法将 ID 附加到文档中?

刚刚修改为使用列表理解

from google.cloud import firestore
db = firestore.Client()
collection_ref = db.collection(u'collection').get()
documents = list(doc.to_dict() for doc in collection_ref)

最新更新