Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to convert the string version of a key back into a form that I can use the get() function on to get the entity instance

class Key(encoded=None) A unique key for a Datastore object.

A key can be converted to a string by passing the Key object to str(). The string is "urlsafe"—it uses only characters valid for use in URLs. The string representation of the key can be converted back to a Key object by passing it to the Key constructor (the encoded argument).

Note: The string representation of a key looks cryptic, but is not encrypted! It can be converted back to the raw key data, both kind and identifier. If you don't want to expose this data to your users (and allow them to easily guess other entities' keys), then encrypt these strings or use something else.

encoded The str form of a Key instance to convert back into a Key.

like image 946
user1769203 Avatar asked Dec 07 '12 03:12

user1769203


2 Answers

In case you are using Python NDB, then you can convert a Key to a URL safe string as follows:

key_str = yourmodel.key.urlsafe()

You can convert back from a URL safe string back to Key as follows:

my_key = ndb.Key(urlsafe=key_str) 

For more information take a look at NDB Key class

like image 90
Thanos Makris Avatar answered Oct 31 '22 04:10

Thanos Makris


If I'm understanding you correctly, you want to take an encoded Key string and convert it back into a Key object. If so, you can do this:

from google.appengine.ext.db import Key

# ...
key_str = '<your_key_string>'    
key_obj = Key(key_str) # or Key(encoded=key_str)

entity = db.get(key_obj) # Although the string will work here as well
like image 33
RocketDonkey Avatar answered Oct 31 '22 06:10

RocketDonkey