Python 2.7 has reached end of support
and will be deprecated
on January 31, 2026. After deprecation, you won't be able to deploy Python 2.7
applications, even if your organization previously used an organization policy to
re-enable deployments of legacy runtimes. Your existing Python
2.7 applications will continue to run and receive traffic after their
deprecation date. We recommend that
you migrate to the latest supported version of Python.
Stay organized with collections
Save and categorize content based on your preferences.
google.appengine.ext.ndb.polymodel module
Summary
Polymorphic models and queries.
The standard NDB Model class only supports ‘functional polymorphism’.
That is, you can create a subclass of Model, and then subclass that
class, as many generations as necessary, and those classes will share
all the same properties and behaviors of their base classes. However,
subclassing Model in this way gives each subclass its own kind. This
means that it is not possible to do ‘polymorphic queries’. Building a
query on a base class will only return entities whose kind matches
that base class’s kind, and exclude entities that are instances of
some subclass of that base class.
The PolyModel class defined here lets you create class hierarchies
that support polymorphic queries. Simply subclass PolyModel instead
of Model.
Contents
class google.appengine.ext.ndb.polymodel.PolyModel(*args, **kwds)source
This class hierarchy has three levels. The first is the ‘root
class’. All models in a single class hierarchy must inherit from
this root. All models in the hierarchy are stored as the same
kind as the root class. For example, Panther entities when stored
to Cloud Datastore are of the kind ‘Animal’. Querying against the
Animal kind will retrieve Cats, Dogs and Canines, for example,
that match your query. Different classes stored in the root
class’ kind are identified by their class key. When loaded from
Cloud Datastore, it is mapped to the appropriate implementation
class.
Polymorphic properties:
Properties that are defined in a given base class within a
hierarchy are stored in Cloud Datastore for all subclasses only.
So, if the Feline class had a property called ‘whiskers’, the Cat
and Panther enties would also have whiskers, but not Animal,
Canine, Dog or Wolf.
Polymorphic queries:
When written to Cloud Datastore, all polymorphic objects
automatically have a property called ‘class’ that you can query
against. Using this property it is possible to easily write a
query against any sub-hierarchy. For example, to fetch only
Canine objects, including all Dogs and Wolves:
Canine.query()
The ‘class’ property is not meant to be used by your code other
than for queries. Since it is supposed to represents the real
Python class it is intended to be hidden from view. (Although if
you feel the need, it is accessible as the ‘class_’ attribute.)
Root class:
The root class is the class from which all other classes of the
hierarchy inherits from. Each hierarchy has a single root class.
A class is a root class if it is an immediate child of PolyModel.
The subclasses of the root class are all the same kind as the root
class. In other words:
Property to store the ‘class key’ of a polymorphic class.
The class key is a list of strings describing a polymorphic entity’s
place within its class hierarchy. This property is automatically
calculated. For example:
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-06-16 UTC."],[[["\u003cp\u003ePolyModel enables the creation of class hierarchies that support polymorphic queries, unlike the standard NDB Model which only allows functional polymorphism.\u003c/p\u003e\n"],["\u003cp\u003eSubclassing PolyModel allows querying across an entire class hierarchy, where all models in the hierarchy are stored under the same kind as the root class.\u003c/p\u003e\n"],["\u003cp\u003eWhen stored, polymorphic objects include a 'class' property, accessible as 'class_', that facilitates querying specific sub-hierarchies within the data.\u003c/p\u003e\n"],["\u003cp\u003eProperties defined in a base class of a PolyModel hierarchy are stored in Cloud Datastore for all its subclasses but not for classes above it in the hierarchy.\u003c/p\u003e\n"],["\u003cp\u003eEach PolyModel hierarchy has a single root class, and all classes within the hierarchy share the same kind as the root class, with unique class names identifying the appropriate subclass.\u003c/p\u003e\n"]]],[],null,["# google.appengine.ext.ndb.polymodel module\n=========================================\n\nSummary\n-------\n\nPolymorphic models and queries.\n\nThe standard NDB Model class only supports 'functional polymorphism'.\nThat is, you can create a subclass of Model, and then subclass that\nclass, as many generations as necessary, and those classes will share\nall the same properties and behaviors of their base classes. However,\nsubclassing Model in this way gives each subclass its own kind. This\nmeans that it is not possible to do 'polymorphic queries'. Building a\nquery on a base class will only return entities whose kind matches\nthat base class's kind, and exclude entities that are instances of\nsome subclass of that base class.\n\nThe PolyModel class defined here lets you create class hierarchies\nthat support polymorphic queries. Simply subclass PolyModel instead\nof Model.\n\nContents\n--------\n\n*class* google.appengine.ext.ndb.polymodel.PolyModel(\\*args, \\*\\*kwds)[source](/appengine/docs/legacy/standard/python/refdocs/modules/google/appengine/ext/ndb/polymodel#PolyModel)\n\n: Bases: [google.appengine.ext.ndb.model.Model](/appengine/docs/legacy/standard/python/refdocs/google.appengine.ext.ndb.model#google.appengine.ext.ndb.model.Model)\n\n Base class for class hierarchies supporting polymorphic queries.\n\n Use this class to build hierarchies that can be queried based on\n their types.\n Example\n\n Consider the following model hierarchy: \n\n +------+\n |Animal|\n +------+\n |\n +-----------------+\n | |\n +------+ +------+\n |Canine| |Feline|\n +------+ +------+\n | |\n +-------+ +-------+\n | | | |\n +---+ +----+ +---+ +-------+\n |Dog| |Wolf| |Cat| |Panther|\n +---+ +----+ +---+ +-------+\n\n This class hierarchy has three levels. The first is the 'root\n class'. All models in a single class hierarchy must inherit from\n this root. All models in the hierarchy are stored as the same\n kind as the root class. For example, Panther entities when stored\n to Cloud Datastore are of the kind 'Animal'. Querying against the\n Animal kind will retrieve Cats, Dogs and Canines, for example,\n that match your query. Different classes stored in the root\n class' kind are identified by their class key. When loaded from\n Cloud Datastore, it is mapped to the appropriate implementation\n class.\n\n Polymorphic properties:\n\n Properties that are defined in a given base class within a\n hierarchy are stored in Cloud Datastore for all subclasses only.\n So, if the Feline class had a property called 'whiskers', the Cat\n and Panther enties would also have whiskers, but not Animal,\n Canine, Dog or Wolf.\n\n Polymorphic queries:\n\n When written to Cloud Datastore, all polymorphic objects\n automatically have a property called 'class' that you can query\n against. Using this property it is possible to easily write a\n query against any sub-hierarchy. For example, to fetch only\n Canine objects, including all Dogs and Wolves: \n\n Canine.query()\n\n The 'class' property is not meant to be used by your code other\n than for queries. Since it is supposed to represents the real\n Python class it is intended to be hidden from view. (Although if\n you feel the need, it is accessible as the 'class_' attribute.)\n\n Root class:\n\n The root class is the class from which all other classes of the\n hierarchy inherits from. Each hierarchy has a single root class.\n A class is a root class if it is an immediate child of PolyModel.\n The subclasses of the root class are all the same kind as the root\n class. In other words: \n\n Animal.kind() == Feline.kind() == Panther.kind() == 'Animal'\n\n | **Note:**\n |\n | All classes in a given hierarchy must have unique names, since\n the class name is used to identify the appropriate subclass. \n\n class_\n\n : Property to store the 'class key' of a polymorphic class.\n\n The class key is a list of strings describing a polymorphic entity's\n place within its class hierarchy. This property is automatically\n calculated. For example: \n\n class Foo(PolyModel): ...\n class Bar(Foo): ...\n class Baz(Bar): ...\n\n Foo().class_ == ['Foo']\n Bar().class_ == ['Foo', 'Bar']\n Baz().class_ == ['Foo', 'Bar', 'Baz']"]]