开发者

Google app engine: query that return entity ID using python

how do I return the entity ID using python in GAE?

Assuming I have following

class Names(db.Model):
   name = db.StringProperty()开发者_高级运维


You retrieve the entity, e.g. with a query, then you call .key().id() on that entity (will be None if the entity has no numeric id; see here for other info you can retrieve from a Key object).


The question has long been answered.
(I am adding some full examples hopefully while not stepping on any toes...)

Getting an entity using a query; just getting the keys is faster and uses less CPU than retrieving the full entity:

query = Names.all(keys_only=True)
names = query.get() # this is a shorter equivalent to `query.fetch(limit=1)`
names.id()

From a template:

{{ names.id }}

GQL alternative, as suggested in a comment:

from google.appengine.ext import db

query = db.GqlQuery("SELECT __key__ FROM Names")
names = query.get()
names.id()
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜