Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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()
like image 727
Peter Avatar asked May 07 '10 04:05

Peter


2 Answers

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).

like image 79
Alex Martelli Avatar answered Oct 26 '22 15:10

Alex Martelli


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()
like image 44
mechanical_meat Avatar answered Oct 26 '22 16:10

mechanical_meat