I'm using Google App Engine with ndb backend and I want to get a single value ex: int from the ndb entity, after any query I get the whole entity with a projection, in my case I have a Transactions entity and I want to sum the amount properties.
What I'm using now is list comprehension on the query result like:
query = ndb.gql("select amount from Transactions").fetch()
result = sum([x.amount for x in query])
Is there any way to make ndb query returns a list of amounts only, so, I can sum the query result directly like:
query = ndb.gql("select amount from Transactions").fetch()
result = sum(query)
Thanks in advance
queryin your code above is in a fact a list of entities and not a query object. So you could reduce the above toresult = reduce(lambda x, y: x+y, map(lambda x: x.amount, Transactions.query().fetch( projection=[Transactions.amount,])))though far less readable than the example below. - Tim Hoffman