Class Key (2.22.0)

Key(*path_args,**kwargs)

An immutable representation of a datastore Key.

.. testsetup:: key-ctor

from google.cloud import datastore

project = 'my-special-pony' client = datastore.Client(project=project) Key = datastore.Key

parent_key = client.key('Parent', 'foo')

To create a basic key directly:

.. doctest:: key-ctor

Key('EntityKind', 1234, project=project) <Key('EntityKind', 1234), project=...>Key('EntityKind', 'foo', project=project) <Key('EntityKind', 'foo'), project=...>

Though typical usage comes via thexref_key factory:

.. doctest:: key-ctor

client.key('EntityKind', 1234) <Key('EntityKind', 1234), project=...>client.key('EntityKind', 'foo') <Key('EntityKind', 'foo'), project=...>

To create a key with a parent:

.. doctest:: key-ctor

client.key('Parent', 'foo', 'Child', 1234) <Key('Parent', 'foo', 'Child', 1234), project=...>client.key('Child', 1234, parent=parent_key) <Key('Parent', 'foo', 'Child', 1234), project=...>

To create a partial key:

.. doctest:: key-ctor

client.key('Parent', 'foo', 'Child') <Key('Parent', 'foo', 'Child'), project=...>

To create a key from a non-default database:

.. doctest:: key-ctor

Key('EntityKind', 1234, project=project, database='mydb') <Key('EntityKind', 1234), project=my-special-pony, database=mydb>

Parameter

NameDescription
path_argstuple of string and integer

May represent a partial (odd length) or full (even length) key path.

Properties

database

Database getter.

Returns
TypeDescription
strThe database of the current key.

flat_path

Getter for the key path as a tuple.

Returns
TypeDescription
tuple of string and integerThe tuple of elements in the path.

id

ID getter. Based on the last element of path.

Returns
TypeDescription
intThe (integer) ID of the key.

id_or_name

Getter. Based on the last element of path.

Returns
TypeDescription
int (ifid) or string (ifname)The last element of the key's path if it is either anid or aname.

is_partial

Boolean indicating if the key has an ID (or name).

Returns
TypeDescription
boolTrue if the last element of the key's path does not have anid or aname.

kind

Kind getter. Based on the last element of path.

Returns
TypeDescription
strThe kind of the current key.

name

Name getter. Based on the last element of path.

Returns
TypeDescription
strThe (string) name of the key.

namespace

Namespace getter.

Returns
TypeDescription
strThe namespace of the current key.

parent

The parent of the current key.

Returns
TypeDescription
Key orNoneTypeA newKey instance, whose path consists of all but the last element of current path. If the current key has only one path element, returnsNone.

path

Path getter.

Returns a copy so that the key remains immutable.

Returns
TypeDescription
list ofdictThe (key) path of the current key.

project

Project getter.

Returns
TypeDescription
strThe key's project.

Methods

__eq__

__eq__(other)

Compare two keys for equality.

Incomplete keys never compare equal to any other key.

Completed keys compare equal if they have the same path, project,database, and namespace.

(Note that database=None is considered to refer to the default database.)

Returns
TypeDescription
boolTrue if the keys compare equal, else False.

__hash__

__hash__()

Hash this key for use in a dictionary lookup.

Returns
TypeDescription
inta hash of the key's state.

__ne__

__ne__(other)

Compare two keys for inequality.

Incomplete keys never compare equal to any other key.

Completed keys compare equal if they have the same path, project,database, and namespace.

(Note that database=None is considered to refer to the default database.)

Returns
TypeDescription
boolFalse if the keys compare equal, else True.

__repr__

__repr__()

String representation of this key.

Includes the project and database, but suppresses them if they areequal to the default values.

completed_key

completed_key(id_or_name)

Creates new key from existing partial key by adding final ID/name.

Parameter
NameDescription
id_or_namestr or integer

ID or name to be added to the key.

Exceptions
TypeDescription
`ValueErrorif the current key is not partial or ifid_or_name is not a string or integer.
Returns
TypeDescription
KeyA newKey instance with the same data as the current one and an extra ID or name added.

from_legacy_urlsafe

from_legacy_urlsafe(urlsafe)
Parameter
NameDescription
urlsafebytes or unicode

The base64 encoded (ASCII) string corresponding to a datastore "Key" / "Reference".

Returns
TypeDescription
Key.The key corresponding tourlsafe.

to_legacy_urlsafe

Parameter
NameDescription
location_prefixstr

The location prefix of an App Engine project ID. Often this value is 's', but may also be 'e', or other location prefixes currently unknown.

Returns
TypeDescription
bytesA bytestring containing the key encoded as URL-safe base64.

to_protobuf

Returns
TypeDescription
.entity_pb2.KeyThe protobuf representing the key.

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025-12-16 UTC.