How to convert a key version string back to a form that I can use the get () function to get an entity instance

class Key (encoded = no) A unique key for the Datastore object.

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

Note. The string representation of the key looks cryptic, but not encrypted! It can be converted back to raw key data, both the view and the identifier. If you do not want to expose this data to your users (and allow them to easily guess the keys of other objects), then encrypt these lines or use something else.

encoded form str of the key instance to convert back to key.

+6
source share
2 answers

If you use Python NDB, you can convert the key to a safe URL string as follows:

key_str = yourmodel.key.urlsafe() 

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

 my_key = ndb.Key(urlsafe=key_str) 

See the NDB Key Class for more information.

+13
source

If I understand you correctly, you want to take the encoded Key string and convert it back to a Key object. If so, you can do the following:

 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 
+3
source

All Articles