Databases Reference
In-Depth Information
In the example where I created
another_task
, I assigned the
start_date
value as 2011/02/01.
This was an arbitrary date I picked just to demonstrate that it could be any valid date value. The
standard Python
datetime.datetime
module is used to create date values in the correct format.
The
datetime.datetime
module, by default, creates and reads dates using the UTC time zone.
You can choose to set the time zone and other attributes using the module's capabilities. This is all
standard Python and you can manipulate dates the Python way that you may be accustomed to.
Next, I revisit the model class and explain a few features that were originally only alluded to in the
code sample. I will also modify the model class to depict a few additional capabilities.
Essentials of Data Modeling for GAE in Python
Although a fi rst rudimentary model class example has already been presented, a slightly more formal
and detailed explanation will be useful. As I explain the details, I will build on what I have already
covered. Look again at the
Task
model class:
class Task(db.Model):
name = db.StringProperty(required=True)
description = db.StringProperty()
start_date = db.DateProperty(required=True)
due_date = db.DateProperty()
end_date = db.DateProperty()
tags = db.StringListProperty()
Available for
download on
Wrox.com
taskmanager GAE project
The fi rst thing to note is that the
Task
model class extends the
db.Model
class. The
Model
class (in
the
google.appengine.ext.db
module) is one of the three built-in model classes provided in the
data modeling API. The other two classes are named
Expando
and
PolyModel
. The
Model
class
is the most rigid and formal of the three model classes. A
Model
defi nes a structured data model,
with a well-defi ned set of properties, where the data types for each of the properties is stated at
design time. In some ways, defi ning a
Model
class or inheriting from it is analogous to defi ning a
traditional database schema.
The
Task
class, which is a
Model
type, defi nes six properties. Each of the six properties have a
well-defi ned type, where the type is defi ned using a subclass of the
Property
class. The Python
wrapper (SDK and API) defi nes and supports a set of property data types. A corresponding set
of classes helps defi ne properties in a data model. A
Property
class defi nes a data type for the
property's value. It also defi nes how values are validated and stored in the data store. For example,
the
StringProperty
class represents all Python
str
or
unicode
value types that are up to 500
characters in length.
DateProperty
, which is a subtype of a
DateTimeProperty
, represents just the
date part of a date and time value.
StringListProperty
represents a list of string values.
You can get a list of all supported value types in a subsection in the online documentation for the
GAE Python API. The subsection is titled “Properties and Values.” You can access the document online
at
http://code.google.com/appengine/docs/python/datastore/entities.html#Properties_
and_Value_Types
. You can access the list of corresponding types and property classes at
http://
code.google.com/appengine/docs/python/datastore/typesandpropertyclasses.html
. The
most common of the supported types and corresponding classes are summarized in Table 10-2.