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.
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With