开发者

Google app engine: query that return entity ID using python

开发者 https://www.devze.com 2022-12-30 00:15 出处:网络
how do I return the entity ID using python in GAE? Assuming I have following class Names(db.Model): name = db.StringProperty()开发者_高级运维

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

精彩评论

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