如何使用python将Firestore查询响应转换为json [英] How to convert firestore query response to json using python

查看:64
本文介绍了如何使用python将Firestore查询响应转换为json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了以下代码:

places_ref = db_client.collection(u'places')
doc = places_ref.where (u'city_name', u'==', 'paris').get()
print(doc)

控制台输出为:

<generator object Query.get at 0x10d6736d8>

如果我尝试这样做:

print(u'Document data: {}'.format(doc.to_dict()))

我收到以下错误:

AttributeError: 'generator' object has no attribute 'to_dict'

如何将查询结果转换为字典?

How do I convert the query result to a dictionary?

推荐答案

firestore的返回值是一个包含许多文档的生成器.查看 firestore文档,看起来下面是您想要的,

The returned value from firestore is a generator containing many documents. Looking at the firestore docs it looks like the following is what you want,

my_dict = { el.id: el.to_dict() for el in doc }

这篇关于如何使用python将Firestore查询响应转换为json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆