English
Skip to page content Skip to main navigation

e.g. "templates" or "datastore"

# The Key Class

An instance of the Key class represents a unique key for a datastore entity.

`Key` is provided by the `google.appengine.ext.db` module.

- Introduction
- Key()
- Class methods:
  - Key.from_path()
- Instance methods:
  - app()
  - kind()
  - id()
  - name()
  - id_or_name()
  - has_id_or_name()
  - parent()

## Introduction

Every model instance that has been stored in the datastore has a unique key that represents the object. The key() method of a model instance returns the Key object for the instance. If the instance has never been put() in the datastore, key() raises a NotSavedError.

An application can retrieve a model instance for a given Key using the get() function.

Key instances can be values for datastore entity properties, including Expando dynamic properties and ListProperty members. The ReferenceProperty model provides features for Key property values such as automatic dereferencing.

## Constructor

class Key(encoded=None)

  A unique key for a datastore object.

  A key can be encoded to a string by passing the Key object to `str()` (or calling the object's `__str__()` method). A string-encoded key is an opaque value using characters safe for including in URLs. The string-encoded key can be converted back to a Key object by passing it to the Key constructor (the encoded argument).

  Note: A string-encoded key can be converted back to the raw key data. This makes it easy to guess other keys when one is known. While string-encoded key values are safe to include in URLs, an application should only do so if key guessability is not an issue.

  encoded
    The `str` form of a Key instance to convert back into a Key.

## Class Methods

The Key class provides the following class method:

Key.from_path(*args, **kwds)

  Builds a new Key object from an ancestor path of one or more entity keys.

  A path represents the hierarchy of parent-child relationships for an entity. Each entity in the path is represented the entity's kind, and either its numeric ID or its key name. The full path represents the entity that appears last in the path, with its ancestors (parents) as preceding entities.

  For example, the following call creates a key for an entity of kind `Address` with the numeric ID `9876` whose parent is an entity of kind `User` with the named key `'Boris'`:

      k = Key.from_path('User', 'Boris', 'Address', 9876)

  Arguments:

  *args
    The path from the root entity to the subject. Each entity in the path is represented by two elements in the list: the name of the kind, and the key name or ID of the entity of that kind.
  **kwds
    Keyword arguments. The method supports one keyword argument, parent, which specifies a parent entity to prepend to the given path. Its value is the parent's Key.

  For more information about paths, see Keys and Entity Groups.

## Instance Methods

Key instances have the following methods:

app()

  Returns the name of the application that stored the data entity.

kind()

  Returns the kind of the data entity, as a string.

id()

  Returns the numeric ID of the data entity, as an integer, or `None` if the entity does not have a numeric ID.

name()

  Returns the name of the data entity, or `None` if the entity does not have a name.

id_or_name()

  Returns the name or numeric ID of the data entity, whichever it has, or `None` if the entity has neither a name nor a numeric ID.

has_id_or_name()

  Returns `True` if the entity has either a name or a numeric ID.

parent()

  Returns the Key of the data entity's parent entity, or `None` if the entity has no parent.