Google Code offered in: English - Español - 日本語 - 한국어 - Português - Pусский - 中文(简体) - 中文(繁體)
An instance of the Key class represents a unique key for a datastore entity.
Key
is provided by the google.appengine.ext.db
module.
Every model instance that has been stored in the datastore has a unique key that represents the object. The key() method of a model instance returns the Key object for the instance. If the instance has never been put() in the datastore, key() raises a NotSavedError.
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.
A unique key for a datastore object.
A key can be encoded to a string by passing the Key object to str()
(or calling the object's __str__()
method). A string-encoded key is an opaque value using characters safe for including in URLs. The string-encoded key can be converted back to a Key object by passing it to the Key constructor (the encoded argument).
Note: A string-encoded key can be converted back to the raw key data. This makes it easy to guess other keys when one is known. While string-encoded key values are safe to include in URLs, an application should only do so if key guessability is not an issue.
str
form of a Key instance to convert back into a Key.The Key class provides the following class method:
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 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 Address
with the numeric ID 9876
whose parent is an entity of kind User
with the named key 'Boris'
:
k = Key.from_path('User', 'Boris', 'Address', 9876)
For more information about paths, see Keys and Entity Groups.
Arguments:
id
, specified as a string or long. It cannot be the number 0.None
, the API uses the current namespace from namespace_manager.get_namespace.None
.Key instances have the following methods:
Returns the name of the application that stored the data entity.
Returns True
if the entity has either a name or a numeric ID.
Returns the numeric ID of the data entity, as an integer, or None
if the entity does not have a numeric ID.
Returns the name or numeric ID of the data entity, whichever it has, or None
if 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 None
if 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 None
if the entity has no parent.