Stay organized with collections Save and categorize content based on your preferences.
This page describes how to use the legacy bundled services and APIs. This API can only run in first-generation runtimes in the App Engine standard environment. If you are updating to the App Engine Python 3 runtime, refer to the migration guide to learn about your migration options for legacy bundled services.An instance of the Key
class represents an immutable Datastore key.
This page has API reference documentation. For an overview, see NDB Entities and Keys.
IntroductionA Key is an immutable Datastore key. Applications normally use them to refer to entities. Any entity that has been stored has a key. To get an entity's key, use the model's key property. To retrieve an entity from its key, call the Key
object's get() method.
Keys support comparisons, for example key1 == key2
or key1 < key2
. These operators compare application ID, namespace, and the full "ancestor path". They use the same ordering as the Datastore uses for queries when ordering by a key property or by key.
repr(key)
or str(key)
returns a string representation resembling the shortest constructor form, omitting the app and namespace unless they differ from the default value.
hash(key)
works. Thus, you can store keys in a hash table.
For flexibility and convenience, multiple constructor signatures are supported.
The positional arguments kind1, id1, kind2, id2... are in "ancestor" order. (This is a shortcut for flat=[kind1, id1, kind2, id2]
) Parents come before children.
The positional-arguments, pairs
, and flat
constructor forms can additionally pass in another key using parent=key. The (kind, id) pairs of the parent key are inserted before the (kind, id) pairs passed explicitly.
The urlsafe
keyword parameter uses a websafe-base64-encoded serialized reference but it's best to think of it as just an opaque unique string.
Additional constructor keyword arguments:
The following methods access the contents of a key. They do not engage in any Datastore I/O activity.
Returns a tuple of (kind, id) pairs.
Returns a tuple of flattened kind and id values (kind1, id1, kind2, id2, ...).
Returns the application id.
Returns the string or integer id in the last (kind, id) pair, or None
if the key is incomplete.
Returns the string id in the last (kind, id) pair, or None
if the key has an integer id or is incomplete.
Returns the integer id in the last (kind, id) pair, or None
if the key has an string id or is incomplete.
Returns the namespace.
Returns the kind in the last (kind, id) pair.
Returns a Key constructed from all but the last (kind, id) pair (or None
if the key has just one (kind, id) pair).
Returns a websafe-base64-encoded serialized version of the key.
Note: The URL-safe string looks cryptic, but it is not encrypted! It can easily be decoded to recover the original entity's kind and identifier.
Returns a Key
for the "old" Datastore API (db
).
These methods interact with the Datastore.
Returns the entity for the Key.
Arguments
Returns a Future
whose eventual result is the entity for the Key.
Arguments
Delete the entity for the Key.
Arguments
Asynchronously delete the entity for the Key.
Arguments
Returns an NDB key from the passed in "old" Datastore API (db
) Key
.
Arguments
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."],[[["This API for bundled services and `Key` class operations is specifically for first-generation runtimes in the App Engine standard environment, with migration to Python 3 requiring a specific guide."],["The `Key` class represents an immutable Datastore key, used to refer to entities and to retrieve them using the `get()` method."],["Keys can be compared using operators such as `==` and `\u003c`, and also support string representation, as well as being used within a hash table."],["The `Key` class offers various constructor signatures, including those for ancestor order, pairs, flat structures, and URL-safe strings, along with options for specifying the application ID and namespace."],["There are multiple methods to access key properties and contents, which do not affect the Datastore, as well as other methods that will interact with the Datastore, like to get or delete an entity."]]],[]]
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