Create / interact with Google Cloud Datastore keys.

class*path_args, **kwargs)[source]#

Bases: object

An immutable representation of a datastore Key.

To create a basic key directly:

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

Though typical usage comes via the key() factory:

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

To create a key with a parent:

>>> 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:

>>> client.key('Parent', 'foo', 'Child')
<Key('Parent', 'foo', 'Child'), project=...>
  • path_args (tuple of string and integer) – May represent a partial (odd length) or full (even length) key path.
  • kwargs (dict) – Keyword arguments to be passed in.

Accepted keyword arguments are

  • namespace (string): A namespace identifier for the key.
  • project (string): The project associated with the key.
  • parent ( The parent of the key.

The project argument is required unless it has been set implicitly.


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

Parameters:id_or_name (str or integer) – ID or name to be added to the key.
Returns:A new Key instance with the same data as the current one and an extra ID or name added.
Raises:ValueError if the current key is not partial or if id_or_name is not a string or integer.

Getter for the key path as a tuple.

Return type:tuple of string and integer
Returns:The tuple of elements in the path.

ID getter. Based on the last element of path.

Return type:int
Returns:The (integer) ID of the key.

Getter. Based on the last element of path.

Return type:int (if id) or string (if name)
Returns:The last element of the key’s path if it is either an id or a name.

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

Return type:bool
Returns:True if the last element of the key’s path does not have an id or a name.

Kind getter. Based on the last element of path.

Return type:str
Returns:The kind of the current key.

Name getter. Based on the last element of path.

Return type:str
Returns:The (string) name of the key.

Namespace getter.

Return type:str
Returns:The namespace of the current key.

The parent of the current key.

Return or NoneType
Returns:A new Key instance, whose path consists of all but the last element of current path. If the current key has only one path element, returns None.

Path getter.

Returns a copy so that the key remains immutable.

Return type:list of dict
Returns:The (key) path of the current key.

Project getter.

Return type:str
Returns:The key’s project.

Return a protobuf corresponding to the key.

Return type:entity_pb2.Key
Returns:The protobuf representing the key.