Skip to content
Advertisement

Get field type of document field from firestore

Is it possible to get the field type of a field in a document?

I want to create a function that looks something like this:

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}")

Where field.type returns the type of the field. For example it should return something like this:

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

Advertisement

Answer

From the document snapshot, with document.get you can get the field value as an Object and then in Python from this object you can the get the type with type() function.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement