Python 3.9 is now generally available.

google.appengine.ext.ndb.Model

A class describing Cloud Datastore entities.

Inherits From: expected_type

Model instances are usually called entities. All model classes inheriting from Model automatically have MetaModel as their metaclass, so that the properties are fixed up properly after the class once the class is defined.

Because of this, you cannot use the same Property object to describe multiple properties -- you must create separate Property objects for each property. E.g. this does not work::

wrong_prop = StringProperty() class Wrong(Model): wrong1 = wrong_prop wrong2 = wrong_prop

The kind is normally equal to the class name (exclusive of the module name or any other parent scope). To override the kind, define a class method named _get_kind(), as follows::

class MyModel(Model): @classmethod def _get_kind(cls): return 'AnotherKind'

key Special property to store the Model key.

Methods

allocate_ids

View source

Allocates a range of key IDs for this model class.

Args
size Number of IDs to allocate. Either size or max can be specified, not both.
max Maximum ID to allocate. Either size or max can be specified, not both.
parent Parent key for which the IDs will be allocated.
**ctx_options Context options.

Returns
A tuple with (start, end) for the allocated range, inclusive.

allocate_ids_async

View source

Allocates a range of key IDs for this model class.

This is the asynchronous version of Model._allocate_ids().

get_by_id

View source

Returns an instance of Model class by ID.

This is really just a shorthand for Key(cls, id, ...).get().

Args
id A string or integer key ID.
parent Optional parent key of the model to get.
namespace Optional namespace.
app Optional app ID.
**ctx_options Context options.

Returns
A model instance or None if not found.

get_by_id_async

View source

Returns an instance of Model class by ID (and app, namespace).

This is the asynchronous version of Model._get_by_id().

get_or_insert

View source

Transactionally retrieves an existing entity or creates a new one.

Positional Args:

  • name: Key name to retrieve or create.

Keyword Args:

  • namespace: Optional namespace.
  • app: Optional app ID.
  • parent: Parent entity key, if any.
  • context_options: ContextOptions object (not keyword args!) or None.
  • **kwds: Keyword arguments to pass to the constructor of the model class if an instance for the specified key name does not already exist. If an instance with the supplied key_name and parent already exists, these arguments will be discarded.

Returns
Existing instance of Model class with the specified key name and parent or a new one that has just been created.

get_or_insert_async

View source

Transactionally retrieves an existing entity or creates a new one.

This is the asynchronous version of Model._get_or_insert().

gql

View source

Run a GQL query.

has_complete_key

View source

Return whether this entity has a complete key.

populate

View source

Populate an instance from keyword arguments.

Each keyword argument will be used to set a corresponding property. Keywords must refer to valid property name. This is similar to passing keyword arguments to the Model constructor, except that no provisions for key, id or parent are made.

put

View source

Write this entity to Cloud Datastore.

If the operation creates or completes a key, the entity's key attribute is set to the new, complete key.

Returns
The key for the entity. This is always a complete key.

put_async

View source

Write this entity to Cloud Datastore.

This is the asynchronous version of Model._put().

query

View source

Create a Query object for this class.

Args
distinct Optional bool, short hand for group_by = projection.
*args Used to apply an initial filter
**kwds are passed to the Query() constructor.

Returns
A Query object.

to_dict

View source

Return a dict containing the entity's property values.

Args
include Optional set of property names to include, default all.
exclude Optional set of property names to skip, default none. A name contained in both include and exclude is excluded.

__eq__

View source

Compare two entities of the same class for equality.

__ne__

View source

Implement self != other as not(self == other).