Reputation: 51
I know how to get all entities by a key using Book.get_by_id(key)
where Book
is an ndb.Model
.
How do I get all the keys within my Kind?
Is it using fetch()
(https://cloud.google.com/appengine/docs/python/ndb/queryclass#Query_fetch) ?
Upvotes: 3
Views: 3975
Reputation: 2537
If you only wish to get all keys just use
entity.query().fetch(key_only=True)
which will return a list of all keys in that entity group. If you wanna get the IDs and not keys you can also use:
map(lambda key: key.id(), entity.query().fetch(key_only=True))
Upvotes: 1
Reputation: 13138
If you only want the keys, use the keys_only
keyword in the fetch()
method:
Book.query().fetch(keys_only=True)
Then you can fetch all the entities using ndb.get_multi(keys)
. According to Guido, this may be more efficient than returning the entities in the query (if the entities are already in the cache).
Upvotes: 6
Reputation: 2536
With all_books = Book.query().fetch()
the all_books
variable will now have every entity of your Book
model.
Note though that when you have lots of entities in the Book
model - it won't be a good idea to load&show them all at once. You will need some kind of pagination implementation (depending on what exactly you're doing) - otherwise your pages will load forever which will create a bad experience for your users.
Read more at https://cloud.google.com/appengine/docs/python/ndb/queries
Upvotes: 5