Class Entity (2.11.1)

Entity(key=None, exclude_from_indexes=())

Entities are akin to rows in a relational database

An entity storing the actual instance of data.

Each entity is officially represented with a xref_Key, however it is possible that you might create an entity with only a partial key (that is, a key with a kind, and possibly a parent, but without an ID). In such a case, the datastore service will automatically assign an ID to the partial key.

Entities in this API act like dictionaries with extras built in that allow you to delete or persist the data stored on the entity.

Entities are mutable and act like a subclass of a dictionary. This means you could take an existing entity and change the key to duplicate the object.

Use xref_get to retrieve an existing entity:

.. testsetup:: entity-ctor

import uuid

from google.cloud import datastore

unique = str(uuid.uuid4())[0:8]
client = datastore.Client(namespace='ns{}'.format(unique))

entity = datastore.Entity(client.key('EntityKind', 1234))
entity['property'] = 'value'
client.put(entity)

.. doctest:: entity-ctor

>>> key = client.key('EntityKind', 1234)
>>> client.get(key)
<Entity('EntityKind', 1234) {'property': 'value'}>

You can the set values on the entity just like you would on any other dictionary.

.. doctest:: entity-ctor

>>> entity['age'] = 20
>>> entity['name'] = 'JJ'

.. testcleanup:: entity-ctor

client.delete(entity.key)

However, not all types are allowed as a value for a Google Cloud Datastore entity. The following basic types are supported by the API:

  • datetime.datetime
  • xref_Key
  • bool
  • float
  • int (as well as long in Python 2)
  • unicode (called str in Python 3)
  • bytes (called str in Python 2)
  • xref_GeoPoint
  • :data:None

In addition, three container types are supported:

  • list
  • xref_Entity
  • dict (will just be treated like an Entity without a key or exclude_from_indexes)

Each entry in a list must be one of the value types (basic or container) and each value in an xref_Entity must as well. In this case an xref_Entity as a container acts as a dict, but also has the special annotations of key and exclude_from_indexes.

And you can treat an entity like a regular Python dictionary:

.. testsetup:: entity-dict

from google.cloud import datastore

entity = datastore.Entity()
entity['age'] = 20
entity['name'] = 'JJ'

.. doctest:: entity-dict

>>> sorted(entity.keys())
['age', 'name']
>>> sorted(entity.items())
[('age', 20), ('name', 'JJ')]

Parameters

NameDescription
key Key

Optional key to be set on entity.

exclude_from_indexes tuple of string

Names of fields whose values are not to be indexed for this entity.

Properties

id

Get the ID of the current entity.

kind

Get the kind of the current entity.

Methods

__eq__

__eq__(other)

Compare two entities for equality.

Entities compare equal if their keys compare equal and their properties compare equal.

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

__ne__

__ne__(other)

Compare two entities for inequality.

Entities compare equal if their keys compare equal and their properties compare equal.

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