4

I have written the following piece of code:

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

The console output is:

<generator object Query.get at 0x10d6736d8>

If I try this:

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

I get the following error:

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

How do I convert the query result to a dictionary?

1 Answer 1

8

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 }
2
  • It returned only one document but this solved my problem. I am not sure how. Oct 25, 2018 at 9:55
  • @AineshPatidar You were attempting to call to_dict on the generator. If you look at the firestore docs again you'll see that they call to_dict on the objects returned by the generator. Oct 25, 2018 at 10:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.