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.
-
strorunicode -
A short string (500 characters or less).
A
strvalue is assumed to be text encoded with theasciicodec, and is converted to aunicodevalue before being stored. The value is returned by the datastore as aunicodevalue. For short strings using other codecs, use aunicodevalue.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
Textinstance. For unencoded byte strings longer than 500 bytes (also not indexed), use aBlobinstance. For non-textual unencoded byte strings up to 500 bytes (not characters) that should be indexed, use aByteStringinstance.Model property:
StringProperty -
bool -
A Boolean value (
TrueorFalse).Model property:
BooleanProperty -
intorlong -
An integer value, up to 64 bits.
Python
intvalues are converted to Pythonlongvalues prior to storage. A value stored as anintwill be returned as along.If a
longlarger 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
datetimemodule documentation .If the
datetimevalue has atzinfoattribute, it will be converted to the UTC time zone for storage. Values come back from the datastore as UTC, with atzinfoofNone. An application that needs date and time values to be in a particular time zone must settzinfocorrectly when updating the value, and convert values to the timezone when accessing the value.Some libraries use the
TZenvironment 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.tzinfoto 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
datetimemodule documentation (includingdatetime.tzinfo). See also the third-party modulepytz, though note that thepytzdistribution has many files.The
DateTimePropertymodel 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 anExpandodynamic property).Model properties:
DateTimeProperty,DateProperty,TimeProperty -
list