【问题标题】:Get field type of document field from firestore从firestore获取文档字段的字段类型
【发布时间】:2021-04-16 14:43:48
【问题描述】:

是否可以获取文档中某个字段的字段类型?

我想创建一个看起来像这样的函数:

def print_field_types(collection_ref, document_ref):
   db = some_function_that_initializes_app()
   document = db.collection(collection_ref).document(document_ref).get().to_dict()
   for field in document:
       print(f"Field_name : {field}, Field_type : {field.type}")

其中 field.type 返回字段的类型。例如,它应该返回如下内容:

Field_name : Username, Field_type : string
Field_name : Age, Field_type : number
Field_name : Tasks, Field_type : array

【问题讨论】:

    标签: python google-cloud-firestore


    【解决方案1】:

    从文档快照中,您可以使用 document.get 将字段值作为对象获取,然后在 Python 中从该对象中您可以使用 type() 函数获取类型。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-09-04
      • 1970-01-01
      • 2021-06-24
      • 1970-01-01
      • 1970-01-01
      • 2022-10-01
      • 2018-10-24
      • 2021-06-05
      相关资源
      最近更新 更多