Google app engine: query returning object id using python

how to return object id using python in GAE?

Assuming I have

class Names(db.Model):
   name = db.StringProperty()
+5
source share
2 answers

You are retrieving an object, for example. with query , you call .key().id()on this object (it will None, if the entity does not have a numeric id, see here for more information that you can get from the object Key).

+13
source

The question has long been given. (I add a few complete examples, hopefully not stepping on any fingers ...)

; CPU, :

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

:

{{ names.id }}

GQL, :

from google.appengine.ext import db

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

All Articles