Stay organized with collections Save and categorize content based on your preferences.
Note: Developers building new applications are strongly encouraged to use the NDB Client Library, which has several benefits compared to this client library, such as automatic entity caching via the Memcache API. If you are currently using the older DB Client Library, read the DB to NDB Migration Guide
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 has an identifying key, which includes the instance's entity kind 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 (put) to the Datastore. The model instance's key()
method returns the Key
object for the instance. If the instance has not yet been assigned a key, 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.
ConstructorA 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.
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 (optional) ancestor path (in an existing Key object), and one or more new path components. Each path component consists of a Kind name (kind
) and an identifier (id_or_name
), which is either a number or a character string.
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 Address
with numeric ID 9876
, under the parent key User/Boris
:
k = Key.from_path('User', 'Boris', 'Address', 9876)
An alternative way of creating the same Key is as follows:
p1 = Key.from_path('User', 'Boris') k = Key.from_path('Address', 9876, parent=p1)
For more information about paths, see the Entities, Properties, and Keys page.
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.
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2025-08-07 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-08-07 UTC."],[[["The Key class represents a unique identifier for a Datastore entity, encompassing its entity kind and a unique identifier which can be either a key name or a numeric ID."],["Developers are encouraged to use the NDB Client Library instead of the older DB Client Library, as NDB offers improved features like automatic entity caching."],["Key objects can be converted to and from \"urlsafe\" string representations, although these strings are not encrypted and reveal the underlying key data."],["The Key.from_path() class method allows the creation of new Key objects by specifying an optional ancestor path and one or more path components, defining the hierarchy of parent-child relationships."],["Key instances provide various methods such as kind(), id(), name(), namespace(), and parent() to access information about the data entity associated with that key."]]],[]]
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4