Mit Sammlungen den Überblick behalten
Sie können Inhalte basierend auf Ihren Einstellungen speichern und kategorisieren.
Eine Instanz der Key-Klasse stellt einen unveränderlichen Datenspeicherschlüssel dar.
Sie finden auf dieser Seite die API-Referenzdokumentation. Eine Übersicht erhalten Sie unter NDB-Entitäten und -Schlüssel.
Einführung
Ein Schlüssel ist ein unveränderlicher Datenspeicherschlüssel. Anwendungen verweisen damit normalerweise auf Entitäten. Jede gespeicherte Entität hat einen Schlüssel. Zum Abrufen des Schlüssels einer Entität verwenden Sie die Schlüssel-Property des Modells.
Mit der Methode get() des Key-Objekts können Sie eine Entität anhand ihres Schlüssels abrufen.
Schlüssel unterstützen Vergleiche, z. B. key1 == key2 oder key1 < key2.
Diese Operatoren vergleichen die Anwendungs-ID, den Namespace und den vollständigen "Ancestor-Pfad". Sie verwenden die gleiche Reihenfolge, die der Datenspeicher für Abfragen verwendet, wenn die Sortierung nach Schlüsselattribut oder Schlüssel erfolgt.
repr(key) oder str(key) gibt eine Stringdarstellung zurück, die der kürzesten Konstruktorform ähnelt, wobei die Anwendung und der Namespace weggelassen werden, sofern sie nicht vom Standardwert abweichen.
hash(key) funktioniert, können Sie Schlüssel in einer Hash-Tabelle speichern.
Konstruktoren
Aus Gründen der Flexibilität und Benutzerfreundlichkeit werden mehrere Konstruktorsignaturen unterstützt.
Die Positionsargumente kind1, id1, kind2, id2... sind nach "Ancestor" sortiert. (Dies ist eine Kurzschreibweise für flat=[kind1, id1,
kind2, id2]). Übergeordnete Elemente stehen vor untergeordneten Elementen.
Die Positionsargumente, pairs, und die Konstruktorformen flat können zusätzlich einen weiteren Schlüssel mit parent=key übergeben. Die Paare (kind, id) des übergeordneten Schlüssels werden eingefügt, bevor die Paare (kind, id) explizit übergeben werden.
Der Schlüsselwortparameter urlsafe verwendet eine mit base64 websicher codierte, serialisierte Referenz. Betrachten Sie ihn jedoch einfach als einen undurchsichtigen eindeutigen String.
Zusätzliche Schlüsselwortargumente für den Konstruktor:
app
Gibt die Anwendungs-ID als String an
Namespace
Gibt den Namespace als String an
Instanzmethoden, die den Datenspeicher nicht beeinflussen
Die folgenden Methoden greifen auf den Inhalt eines Schlüssels zu. Sie führen keine E/A-Datenspeicheraktivitäten aus.
pairs()
Gibt ein Tupel von (kind, id)-Paaren zurück
flat()
Gibt ein Tupel von vereinfachten Typ- und ID-Werten (kind1, id1, kind2, id2, …) zurück
app()
Gibt die Anwendungs-ID zurück
id()
Gibt die String- oder Ganzzahl-ID im letzten (kind, id)-Paar oder None zurück, falls der Schlüssel unvollständig ist
string_id()
Gibt die String-ID im letzten (kind, id)-Paar oder None zurück, falls der Schlüssel eine Ganzzahl-ID hat oder unvollständig ist
integer_id()
Gibt die Ganzzahl-ID im letzten (kind, id)-Paar oder None zurück, falls der Schlüssel eine String-ID hat oder unvollständig ist
namespace()
Gibt den Namespace zurück
kind()
Gibt den Typ im letzten (kind, id)-Paar zurück
parent()
Gibt einen Schlüssel zurück, der aus allen (kind, id)-Paaren mit Ausnahme des letzten besteht, oder None, wenn der Schlüssel nur ein (kind, id)-Paar hat
urlsafe()
Gibt eine mit base64 websicher codierte, serialisierte Version des Schlüssels zurück
Hinweis:
Der URL-sichere Schlüssel wirkt verschlüsselt, was jedoch nicht der Fall ist.
Er lässt sich einfach entschlüsseln, um den Typ und die ID der ursprünglichen Entität wiederherzustellen.
to_old_key()
Gibt einen Key für die "alte" Datastore-API zurück (db).
Instanzmethoden mit Auswirkungen auf den Datenspeicher
Diese Methoden interagieren mit dem Datenspeicher.
[[["Leicht verständlich","easyToUnderstand","thumb-up"],["Mein Problem wurde gelöst","solvedMyProblem","thumb-up"],["Sonstiges","otherUp","thumb-up"]],[["Schwer verständlich","hardToUnderstand","thumb-down"],["Informationen oder Beispielcode falsch","incorrectInformationOrSampleCode","thumb-down"],["Benötigte Informationen/Beispiele nicht gefunden","missingTheInformationSamplesINeed","thumb-down"],["Problem mit der Übersetzung","translationIssue","thumb-down"],["Sonstiges","otherDown","thumb-down"]],["Zuletzt aktualisiert: 2025-09-04 (UTC)."],[[["\u003cp\u003eThis API for bundled services and \u003ccode\u003eKey\u003c/code\u003e class operations is specifically for first-generation runtimes in the App Engine standard environment, with migration to Python 3 requiring a specific guide.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003eKey\u003c/code\u003e class represents an immutable Datastore key, used to refer to entities and to retrieve them using the \u003ccode\u003eget()\u003c/code\u003e method.\u003c/p\u003e\n"],["\u003cp\u003eKeys can be compared using operators such as \u003ccode\u003e==\u003c/code\u003e and \u003ccode\u003e<\u003c/code\u003e, and also support string representation, as well as being used within a hash table.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003eKey\u003c/code\u003e 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.\u003c/p\u003e\n"],["\u003cp\u003eThere 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.\u003c/p\u003e\n"]]],[],null,["# NDB Key Class\n\n| 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](/appengine/migration-center/standard/migrate-to-second-gen/python-differences) to learn about your migration options for legacy bundled services.\n\nAn instance of the `Key` class represents an immutable\nDatastore key.\n\nThis page has API reference documentation. For an overview,\nsee [NDB Entities and Keys](/appengine/docs/legacy/standard/python/ndb/creating-entities).\n\nIntroduction\n------------\n\nA Key is an immutable Datastore key. Applications normally use them to refer\nto entities. Any entity that has been stored has a key. To get an entity's\nkey, use the model's [key](/appengine/docs/legacy/standard/python/ndb/modelclass#Model_key)\nproperty.\nTo retrieve an entity from its key, call the `Key`\nobject's [get()](#Key_get) method.\n\nKeys support comparisons, for example\n\u003cvar translate=\"no\"\u003ekey1\u003c/var\u003e` == `\u003cvar translate=\"no\"\u003ekey2\u003c/var\u003e or\n\u003cvar translate=\"no\"\u003ekey1\u003c/var\u003e` \u003c `\u003cvar translate=\"no\"\u003ekey2\u003c/var\u003e.\nThese operators compare application ID, namespace, and the full\n\"ancestor path\". They use the same ordering as the Datastore uses\nfor queries when ordering by a key property or by key.\n\n`repr(`\u003cvar translate=\"no\"\u003ekey\u003c/var\u003e`)` or\n`str(`\u003cvar translate=\"no\"\u003ekey\u003c/var\u003e`)` returns\na string representation resembling\nthe shortest constructor form, omitting the app and namespace\nunless they differ from the default value.\n\n`hash(`\u003cvar translate=\"no\"\u003ekey\u003c/var\u003e`)` works. Thus, you can store keys in\na hash table.\n\nConstructors\n------------\n\nFor flexibility and convenience, multiple constructor signatures are\nsupported.\n\nclass Key(\u003cvar translate=\"no\"\u003ekind1\u003c/var\u003e, \u003cvar translate=\"no\"\u003eid1\u003c/var\u003e, \u003cvar translate=\"no\"\u003ekind2\u003c/var\u003e, \u003cvar translate=\"no\"\u003eid2\u003c/var\u003e, ...)\nclass Key(pairs=\\[(\u003cvar translate=\"no\"\u003ekind1\u003c/var\u003e, \u003cvar translate=\"no\"\u003eid1\u003c/var\u003e), (\u003cvar translate=\"no\"\u003ekind2\u003c/var\u003e, \u003cvar translate=\"no\"\u003eid2\u003c/var\u003e), ...\\])\nclass Key(flat=\\[\u003cvar translate=\"no\"\u003ekind1\u003c/var\u003e, \u003cvar translate=\"no\"\u003eid1\u003c/var\u003e, \u003cvar translate=\"no\"\u003ekind2\u003c/var\u003e, \u003cvar translate=\"no\"\u003eid2\u003c/var\u003e, ...\\])\nclass Key(urlsafe=\u003cvar translate=\"no\"\u003estring\u003c/var\u003e)\n\n: The positional arguments\n \u003cvar translate=\"no\"\u003ekind1\u003c/var\u003e, \u003cvar translate=\"no\"\u003eid1\u003c/var\u003e, \u003cvar translate=\"no\"\u003ekind2\u003c/var\u003e, \u003cvar translate=\"no\"\u003eid2\u003c/var\u003e...\n are in \"ancestor\" order. (This is a shortcut for\n `flat=[`\u003cvar translate=\"no\"\u003ekind1\u003c/var\u003e`, `\u003cvar translate=\"no\"\u003eid1\u003c/var\u003e`,\n `\u003cvar translate=\"no\"\u003ekind2\u003c/var\u003e`, `\u003cvar translate=\"no\"\u003eid2\u003c/var\u003e`]`) Parents come before children.\n\n The positional-arguments, `pairs`, and `flat`\n constructor forms can additionally pass in another\n key using parent=\u003cvar translate=\"no\"\u003ekey\u003c/var\u003e.\n The (\u003cvar translate=\"no\"\u003ekind\u003c/var\u003e, \u003cvar translate=\"no\"\u003eid\u003c/var\u003e) pairs of the parent key are\n inserted before the (\u003cvar translate=\"no\"\u003ekind\u003c/var\u003e, \u003cvar translate=\"no\"\u003eid\u003c/var\u003e) pairs\n passed explicitly.\n\n The `urlsafe` keyword parameter uses\n a websafe-base64-encoded serialized\n reference\n but it's best to think of it as just an opaque unique\n string.\n\n Additional constructor keyword arguments:\n\n app\n : specify the application id (a string)\n\n namespace\n : specify the namespace (a string)\n\nInstance Methods that Don't Affect the Datastore\n------------------------------------------------\n\nThe following methods access the contents of a key. They do not\nengage in any Datastore I/O activity.\n\npairs()\n\n: Returns a tuple of (kind, id) pairs.\n\nflat()\n\n: Returns a tuple of flattened kind and id values\n (kind1, id1, kind2, id2, ...).\n\napp()\n\n: Returns the application id.\n\nid()\n\n: Returns the string or integer id in the last (kind, id) pair,\n or `None` if the key is incomplete.\n\nstring_id()\n\n: Returns the string id in the last (kind, id) pair,\n or `None` if the key has an integer id or is incomplete.\n\ninteger_id()\n\n: Returns the integer id in the last (kind, id) pair,\n or `None` if the key has an string id or is incomplete.\n\nnamespace()\n\n: Returns the namespace.\n\nkind()\n\n: Returns the kind in the last (kind, id) pair.\n\nparent()\n\n: Returns a Key constructed from all but the last (kind, id) pair\n (or `None` if the key has just one (kind, id) pair).\n\nurlsafe()\n\n: Returns a websafe-base64-encoded serialized version of the key.\n\n **Note:**\n The URL-safe string looks cryptic, but it is not encrypted!\n It can easily be decoded to recover the original entity's kind and\n identifier.\n\nto_old_key()\n\n: Returns a\n [Key](/appengine/docs/legacy/standard/python/datastore/keyclass)\n for the \"old\" Datastore API (`db`).\n\nInstance Methods that Affect the Datastore\n------------------------------------------\n\nThese methods interact with the Datastore.\n\nget(\\*\\*ctx_options)\n\n: Returns the entity for the Key.\n\n **Arguments**\n\n \\*\\*ctx_options\n : [Context options](/appengine/docs/legacy/standard/python/ndb/functions#context_options)\n\nget_async(\\*\\*ctx_options)\n\n: Returns a\n [Future](/appengine/docs/legacy/standard/python/ndb/futureclass)\n whose eventual result is\n the entity for the Key.\n\n **Arguments**\n\n \\*\\*ctx_options\n : [Context options](/appengine/docs/legacy/standard/python/ndb/functions#context_options)\n\ndelete(\\*\\*ctx_options)\n\n: Delete the entity for the Key.\n\n **Arguments**\n\n \\*\\*ctx_options\n : [Context options](/appengine/docs/legacy/standard/python/ndb/functions#context_options)\n\ndelete_async(\\*\\*ctx_options)\n\n: Asynchronously delete the entity for the Key.\n\n **Arguments**\n\n \\*\\*ctx_options\n : [Context options](/appengine/docs/legacy/standard/python/ndb/functions#context_options)\n\nClass Methods\n-------------\n\nfrom_old_key(k)\n\n: Returns an NDB key from the passed in \"old\" Datastore API\n (`db`)\n [Key](/appengine/docs/legacy/standard/python/datastore/keyclass).\n\n **Arguments**\n\n \\*\\*ctx_options\n : [Context options](/appengine/docs/legacy/standard/python/ndb/functions#context_options)"]]