An instance of the Key class represents a unique key for a Datastore entity.
Key is provided by the
- Class methods:
- Instance methods:
Every model instance has an identifying
which includes the instance's
along with a unique identifier. The identifier may be either a key name string, assigned explicitly by the application when the instance is created, or an integer numeric ID, assigned automatically by App Engine when the instance is written
to the Datastore. The model instance's
method returns the
Key object for the instance. If the instance has not yet been assigned a key,
key() raises a
An application can retrieve a model instance for a given Key using the get() function.
Key instances can be values for Datastore entity properties, including Expando dynamic properties and ListProperty members. The ReferenceProperty model provides features for Key property values such as automatic dereferencing.
- 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.
strform of a Key instance to convert back into a Key.
The Key class provides the following class method:
- Key.from_path(kind, id_or_name, parent=None, namespace=None)
Builds a new Key object from an ancestor path of one or more entity keys.
A path represents the hierarchy of parent-child relationships for an entity. Each entity in the path is represented by the entity's kind, and either its numeric ID or its key name. The full path represents the entity that appears last in the path, with its ancestors (parents) as preceding entities.
For example, the following call creates a key for an entity of kind
Addresswith the numeric ID
9876whose parent is an entity of kind
Userwith the key name
k = Key.from_path('User', 'Boris', 'Address', 9876)
For more information about paths, see the Entities, Properties, and Keys page.
- The entity kind, represented as a string or a Unicode string.
id, specified as a string or long. It cannot be the number 0.
- The namespace to set for this Key only. If you supply a namespace here, it overrides the current namespace set in the namespace_manager. If
None, the API uses the current namespace from namespace_manager.get_namespace.
- Optional parent key. If not supplied, defaults to
Key instances have the following methods:
Returns the name of the application that stored the data entity.
Trueif the entity has either a name or a numeric ID.
Returns the numeric ID of the data entity, as an integer, or
Noneif the entity does not have a numeric ID.
Returns the name or numeric ID of the data entity, whichever it has, or
Noneif the entity has neither a name nor a numeric ID.
Returns the kind of the data entity, as a string.
Returns the name of the data entity, or
Noneif the entity does not have a name.
Returns the namespace of the data entity. If the entity does not have a current namespace, this method returns the current namespace set in the namespace_manager.
Returns the Key of the data entity's parent entity, or
Noneif the entity has no parent.