Get NDB Request Length - Using Python in Google App Engine

What is a good way to get the number of query results when using NDB in the Google engine?

Attempt:

query = NDB_Model.query(NDB_Model.some_property == some_value)
if len(query) > 0:    # <-- this throws and exception
    entity = query[0]

I apologize that this is probably a very simple question, but it did not seem to me from the docs .

+5
source share
2 answers

It looks like you just want to get the first object from your request. What query.get()for.

query = NDB_Model.query(NDB_Model.some_property == some_value)

entity = query.get()
if entity is not None:
    # Do stuff

From the docs:

Returns the first result of the request, if any (otherwise None). This is similar to calling q.fetch (1) and returning the first element of the result list.

query.fetch(n), n - . , len() .

+11

ndb, count():

query = NDB_Model.query(NDB_Model.some_property == some_value)
if query.count() > 0:
    entity = query[0]
+3

All Articles