The App Engine datastore supports a fixed set of value types for properties on data entities.
Property
classes can define new types that are converted to and from the underlying value types, and the value types can be used directly with
Expando
dynamic properties and
ListProperty
aggregate property models.
The following table describes the Property classes whose values correspond directly with the underlying data types. Any of these value types can be used in an Expando dynamic property or ListProperty aggregate type.
Datastore Value Types
Datastore entity property values can be of one of the following types. See above for a list of corresponding
Property
classes to use with
Model
definitions.
Other than the Python standard types and
users.User
,
all classes described in this section are provided by the
google.appengine.ext.db
module.
-
str
orunicode
-
A short string (500 characters or less).
A
str
value is assumed to be text encoded with theascii
codec, and is converted to aunicode
value before being stored. The value is returned by the datastore as aunicode
value. For short strings using other codecs, use aunicode
value.Short strings are indexed by the datastore, and can be used in filters and sort orders. For text strings longer than 500 characters (which are not indexed), use a
Text
instance. For unencoded byte strings longer than 500 bytes (also not indexed), use aBlob
instance. For non-textual unencoded byte strings up to 500 bytes (not characters) that should be indexed, use aByteString
instance.Model property:
StringProperty
-
bool
-
A Boolean value (
True
orFalse
).Model property:
BooleanProperty
-
int
orlong
-
An integer value, up to 64 bits.
Python
int
values are converted to Pythonlong
values prior to storage. A value stored as anint
will be returned as along
.If a
long
larger than 64 bits is assigned, only the least significant 64 bits are stored.Model property:
IntegerProperty
-
float
-
A floating-point number.
Model property:
FloatProperty
-
datetime.datetime
-
A date and time. See the
datetime
module documentation .If the
datetime
value has atzinfo
attribute, it will be converted to the UTC time zone for storage. Values come back from the datastore as UTC, with atzinfo
ofNone
. An application that needs date and time values to be in a particular time zone must settzinfo
correctly when updating the value, and convert values to the timezone when accessing the value.Some libraries use the
TZ
environment variable to control the time zone applied to date-time values. App Engine sets this environment variable to"UTC"
. Note that changing this variable in an application will not change the behavior of some datetime functions, because changes to environment variables are not visible outside of the Python code.If you only convert values to and from a particular time zone, you can implement a custom
datetime.tzinfo
to convert values from the datastore:import datetime import time class Pacific_tzinfo(datetime.tzinfo): """Implementation of the Pacific timezone.""" def utcoffset(self, dt): return datetime.timedelta(hours=-8) + self.dst(dt) def _FirstSunday(self, dt): """First Sunday on or after dt.""" return dt + datetime.timedelta(days=(6-dt.weekday())) def dst(self, dt): # 2 am on the second Sunday in March dst_start = self._FirstSunday(datetime.datetime(dt.year, 3, 8, 2)) # 1 am on the first Sunday in November dst_end = self._FirstSunday(datetime.datetime(dt.year, 11, 1, 1)) if dst_start <= dt.replace(tzinfo=None) < dst_end: return datetime.timedelta(hours=1) else: return datetime.timedelta(hours=0) def tzname(self, dt): if self.dst(dt) == datetime.timedelta(hours=0): return "PST" else: return "PDT" pacific_time = datetime.datetime.fromtimestamp(time.mktime(utc_time.timetuple()), Pacific_tzinfo())
See the
datetime
module documentation (includingdatetime.tzinfo
). See also the third-party modulepytz
, though note that thepytz
distribution has many files.The
DateTimeProperty
model property class includes features such as the ability to automatically use the date and time a model instance is stored. These are features of the model, and are not available on the raw datastore value (such as in anExpando
dynamic property).Model properties:
DateTimeProperty
,DateProperty
,TimeProperty
-
list