Entities#
Class for representing a single entity in the Cloud Datastore.
-
class
google.cloud.datastore.entity.
Entity
(key=None, exclude_from_indexes=())[source]# Bases:
dict
Entities are akin to rows in a relational database
An entity storing the actual instance of data.
Each entity is officially represented with a
google.cloud.datastore.key.Key
class, 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
google.cloud.datastore.get()
to retrieve an existing entity.>>> from google.cloud import datastore >>> client = datastore.Client() >>> client.get(key) <Entity[{'kind': 'EntityKind', id: 1234}] {'property': 'value'}>
You can the set values on the entity just like you would on any other dictionary.
>>> entity['age'] = 20 >>> entity['name'] = 'JJ' >>> entity <Entity[{'kind': 'EntityKind', id: 1234}] {'age': 20, 'name': 'JJ'}>
And you can convert an entity to a regular Python dictionary with the
dict
builtin:>>> dict(entity) {'age': 20, 'name': 'JJ'}
Note
When saving an entity to the backend, values which are “text” (
unicode
in Python2,str
in Python3) will be saved using the ‘text_value’ field, after being encoded to UTF-8. When retrieved from the back-end, such values will be decoded to “text” again. Values which are “bytes” (str
in Python2,bytes
in Python3), will be saved using the ‘blob_value’ field, without any decoding / encoding step.Parameters: - key (
google.cloud.datastore.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.
-
exclude_from_indexes
# Names of fields which are not to be indexed for this entity.
Return type: sequence of field names Returns: The set of fields excluded from indexes.
-
kind
# Get the kind of the current entity.
Note
This relies entirely on the
google.cloud.datastore.key.Key
set on the entity. That means that we’re not storing the kind of the entity at all, just the properties and a pointer to a Key which knows its Kind.
- key (