How to get a single value from gae ndb query?
<p>I'm using Google App Engine with ndb backend and I want to get a single value ex: <code>int</code> 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.</p>

<p>What I'm using now is list comprehension on the query result like:</p>

<pre><code>query = ndb.gql("select amount from Transactions").fetch()
result = sum([x.amount for x in query])
</code></pre>

<p>Is there any way to make ndb query returns a list of amounts only, so, I can sum the query result directly like:</p>

<pre><code>query = ndb.gql("select amount from Transactions").fetch()
result = sum(query)
</code></pre>

<p>Thanks in advance</p>

<p>There is no direct way to get list of values from projection fetch (I suppose you are looking for ndb analog of django orm Transactions.query().all().values()), you may do as follows:</p>

<pre><code>result = sum([x.amount for x in Transactions.query().fetch(
    projection=[Transactions.amount,])])
</code></pre>

<p><a href="https://cloud.google.com/appengine/docs/python/ndb/projectionqueries" rel="nofollow">https://cloud.google.com/appengine/docs/python/ndb/projectionqueries</a></p>

python google-app-engine app-engine-ndb gql gqlquery