mrblue
mrblue

Reputation: 819

How I find the total entities from a query, using Google Datastore

I have a Python script like this to work with Google Cloud DataStore

def implicit():
  query = client.query(kind='task')
  query.add_filter('status', '=', True)
  query_iter = query.fetch()
  print(query_iter.num_results)
  for entity in query_iter:
    print(entity['title'])

I've inserted 5 'task' entities to DataStore already, and it still print out the titles however, I got the query_iter.num_results is 0, it supposed to be 5 instead. Is there anything wrong with my code.

Thanks and Regards

Upvotes: 0

Views: 514

Answers (1)

Jim Morrison
Jim Morrison

Reputation: 2887

num_results will return how many results from the iterator you have consumed. So, to get the total number of results before iterating over the results you need to convert the iterator to a list, i.e. query_results = list(query.fetch()). Then len(query_results) will have the total number of results.

Upvotes: 1

Related Questions