Serialize an entity key for a string in Python for GAE

There is a way in the low-level Java API to turn an entity key into a string so that you can pass it to the client via JSON if you want. Is there any way to do this for python?

+5
source share
4 answers

Depending on whether you use keywords or not, obj.key().name()or obj.key().id()you can use to get a name or identifier, respectively. None of them contain the name of the entity class, so they are not enough to retrieve the source object from the data store. Of course, in most cases, you usually know the type of entity when working with it, so as not to be a problem.

, (keynames ), obj.key().id_or_name(). :

from google.appengine.ext import db
#...
obj_key = db.Key.from_path('EntityClass', id_or_name)
obj = db.get(obj_key)

, , (, GAE), (str(obj.key()) db.get .

+3

str(entity.key()) base64.

entity.key().name() entity.key().id() , .

+3

:

string_key = entity.key().urlsafe()

,

key = ndb.Key(urlsafe=string_key)
+2

:

entity.key().name()

. .

, ?

+1

All Articles