Base-class for models that supports polymorphic queries.
Use this class to build hierarchies that can be queried based
on their types.
Example:
consider the following model hierarchy:
+------+
|Animal|
+------+
|
+-----------------+
| |
+------+ +------+
|Canine| |Feline|
+------+ +------+
| |
+-------+ +-------+
| | | |
+---+ +----+ +---+ +-------+
|Dog| |Wolf| |Cat| |Panther|
+---+ +----+ +---+ +-------+
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 the 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 the 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 the datastore for all sub-casses 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 the 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 GQL query against any sub-hierarchy. For
example, to fetch only Canine objects, including all Dogs and Wolves:
db.GqlQuery("SELECT * FROM Animal WHERE class='Canine'")
And alternate method is to use the 'all' or 'gql' methods of the Canine
class:
Canine.all()
Canine.gql('')
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.
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:
Animal.kind() == Feline.kind() == Panther.kind() == 'Animal'
def google.appengine.ext.db.polymodel.PolyModel.class_name |
( |
|
cls | ) |
|
Calculate class name for this class.
Returns name to use for each classes element within its class-key. Used
to discriminate between different classes within a class hierarchy's
Datastore kind.
The presence of this method allows developers to use a different class
name in the datastore from what is used in Python code. This is useful,
for example, for renaming classes without having to migrate instances
already written to the datastore. For example, to rename a polymorphic
class Contact to SimpleContact, you could convert:
# Class key is ['Information']
class Information(PolyModel): ...
# Class key is ['Information', 'Contact']
class Contact(Information): ...
to:
# Class key is still ['Information', 'Contact']
class SimpleContact(Information):
...
@classmethod
def class_name(cls):
return 'Contact'
# Class key is ['Information', 'Contact', 'ExtendedContact']
class ExtendedContact(SimpleContact): ...
This would ensure that all objects written previously using the old class
name would still be loaded.
Returns:
Name of this class.