google.appengine.ext.ndb.Key

An immutable datastore key.

Inherits From: expected_type

For flexibility and convenience, multiple constructor signatures are supported.

The primary way to construct a key is using positional arguments:

  • Key(kind1, id1, kind2, id2, ...).

This is shorthand for either of the following two longer forms:

  • Key(pairs=[(kind1, id1), (kind2, id2), ...])
  • Key(flat=[kind1, id1, kind2, id2, ...])

Either of the above constructor forms can additionally pass in another key using parent=. The (kind, id) pairs of the parent key are inserted before the (kind, id) pairs passed explicitly.

You can also construct a Key from a 'url-safe' encoded string:

  • Key(urlsafe=)

For esoteric purposes the following constructors exist:

  • Key(reference=) -- passing in a low-level Reference object
  • Key(serialized=) -- passing in a serialized low-level Reference
  • Key() -- for unpickling, the same as Key(**)

The 'url-safe' string is really a websafe-base64-encoded serialized Reference, but it's best to think of it as just an opaque unique string.

Additional constructor keyword arguments:

  • app= -- specify the application id
  • namespace= -- specify the namespace

If a Reference is passed (using one of reference, serialized or urlsafe), the args and namespace keywords must match what is already present in the Reference (after decoding if necessary). The parent keyword cannot be combined with a Reference in any form.

Keys are immutable, which means that a Key object cannot be modified once it has been created. This is enforced by the implementation as well as Python allows.

For access to the contents of a key, the following methods and operations are supported:

  • repr(key), str(key) -- return a string representation resembling the shortest constructor form, omitting the app and namespace unless they differ from the default value.

  • key1 == key2, key1 != key2 -- comparison for equality between Keys.

  • hash(key) -- a hash value sufficient for storing Keys in a dict.

  • key.pairs() -- a tuple of (kind, id) pairs.

  • key.flat() -- a tuple of flattened kind and id values, i.e. (kind1, id1, kind2, id2, ...).

  • key.app() -- the application id.

  • key.id() -- the string or integer id in the last (kind, id) pair, or None if the key is incomplete.

  • key.string_id() -- the string id in the last (kind, id) pair, or None if the key has an integer id or is incomplete.

  • key.integer_id() -- the integer id in the last (kind, id) pair, or None if the key has a string id or is incomplete.

  • key.namespace() -- the namespace.

  • key.kind() -- a shortcut for key.pairs()[-1][0].

  • key.parent() -- a Key constructed from all but the last (kind, id) pairs.

  • key.urlsafe() -- a websafe-base64-encoded serialized Reference.

  • key.serialized() -- a serialized Reference.

  • key.reference() -- a Reference object. The caller promises not to mutate it.

Keys also support interaction with the datastore; these methods are the only ones that engage in any kind of I/O activity. For Future objects, see the document for ndb/tasklets.py.

  • key.get() -- return the entity for the Key.

  • key.get_async() -- return a Future whose eventual result is the entity for the Key.

  • key.delete() -- delete the entity for the Key.

  • key.delete_async() -- asynchronously delete the entity for the Key.

Keys may be pickled.

Subclassing Key is best avoided; it would be hard to get right.

Methods

app

View source

Return the application id.

delete

View source

Synchronously delete the entity for this Key.

This is a no-op if no such entity exists.

delete_async

View source

Schedule deletion of the entity for this Key.

This returns a Future, whose result becomes available once the deletion is complete. If no such entity exists, a Future is still returned. In all cases the Future's result is None (i.e. there is no way to tell whether the entity existed or not).

flat

View source

Return a tuple of alternating kind and id values.

from_old_key

View source

get

View source

Synchronously get the entity for this Key.

Return None if there is no such entity.

get_async

View source

Return a Future whose result is the entity for this Key.

If no such entity exists, a Future is still returned, and the Future's eventual return result be None.

id

View source

Return the string or integer id in the last (kind, id) pair, if any.

Returns
A string or integer id, or None if the key is incomplete.

integer_id

View source

Return the integer id in the last (kind, id) pair, if any.

Returns
An integer id, or None if the key has a string id or is incomplete.

kind

View source

Return the kind of the entity referenced.

This is the kind from the last (kind, id) pair.

namespace

View source

Return the namespace.

pairs

View source

Return a tuple of (kind, id) pairs.

parent

View source

Return a Key constructed from all but the last (kind, id) pairs.

If there is only one (kind, id) pair, return None.

reference

View source

Return the Reference object for this Key.

This is a entity_pb2.Reference instance -- a protocol buffer class used by the lower-level API to the datastore.

NOTE: The caller should not mutate the return value.

root

View source

Return the root key. This is either self or the highest parent.

serialized

View source

Return a serialized Reference object for this Key.

string_id

View source

Return the string id in the last (kind, id) pair, if any.

Returns
A string id, or None if the key has an integer id or is incomplete.

to_old_key

View source

urlsafe

View source

Return a url-safe string encoding this Key's Reference.

This string is compatible with other APIs and languages and with the strings used to represent Keys in GQL and in the App Engine Admin Console.

__eq__

View source

Return self==value.

__ge__

View source

Return self>=value.

__gt__

View source

Return self>value.

__le__

View source

Return self<=value.

__lt__

View source

Return self<value.

__ne__

View source

Return self!=value.