【问题标题】:Firestore DocumentSnapshot to Python dictionaryFirestore DocumentSnapshot 到 Python 字典
【发布时间】:2018-03-02 21:06:48
【问题描述】:

您好,我一直在兜圈子,试图将我的 Firestore 数据放入 Python 2 字典中。

    doc_ref = db.collection('things1').document('ref1').collection('things2').document('ref2')
    doc = doc_ref.get()

给我一​​个 DocumentSnapshot - 我希望得到一个字典。从结果中创建字典的正确方法是什么。尝试了文档并最终得到了该对象。我正在(不)在做一些愚蠢的事情。

谢谢

【问题讨论】:

    标签: python google-cloud-firestore


    【解决方案1】:

    你可以使用to_dict(),这应该会给你结果的字典。

    doc_ref = db.collection('things1').document('ref1').collection('things2').document('ref2') 
    doc = doc_ref.get().to_dict()
    

    【讨论】:

    • 为什么这突然不是正确答案? @Townheadbluesboy?
    【解决方案2】:

    这对我有用。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-07
      • 2021-02-26
      • 2020-09-14
      • 2021-01-10
      • 1970-01-01
      • 2018-09-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多