Fraser Harris
Fraser Harris

Reputation: 1942

gqlQuery returns object, want list of keys

Is there a way to convert the GqlQuery object to an array of keys, or is there a way to force the query to return an array of keys? For example:

items = db.GqlQuery("SELECT __key__ FROM Items")

returns an object containing the keys:

<google.appengine.ext.db.GqlQuery object at 0x0415E210>

I need to compare it to an array of keys that look like:

[datastore_types.Key.from_path(u'Item', 100L, _app_id_namespace=u'items'),
 ..., datastore_types.Key.from_path(u'Item', 105L, _app_id_namespace=u'fitems')]

Note: I can get around the problem by querying for the stored objects, and then calling .key(), but this seems wasteful.

items = db.GqlQuery("SELECT * FROM Items")
keyArray = []
for item in items:
  keyArray.append(item.key())

Upvotes: 1

Views: 1319

Answers (1)

Nick Johnson
Nick Johnson

Reputation: 101149

Certainly - you can fetch the results by calling .fetch(count) on the GqlQuery object. This is the recommended way, in fact - iterating fetches results in batches, and so is less efficient.

Upvotes: 3

Related Questions