The property declaration classes serve several functions in your data model:
The model calls the class when a value is assigned to the property, and the class can raise an exception if the value does not meet its conditions.
The model calls the class to convert from the value type used by the app to one of the core datastore types for storage, and back again.
The model calls the class if no value was assigned to determine an appropriate default value.
Every property declaration class inherits from the
db.Property base class. This class implements features common
to all property declarations, including support for the common constructor
arguments (such as
indexed). Declaration classes override methods and members to
specialize the validation and type conversion routines.
Here is a very simple property declaration class. It accepts any string value, and stores it as a datastore short string (the default behavior for Python string values).
from google.appengine.ext import db class PlayerNameProperty(db.Property): data_type = basestring def validate(self, value): value = super(PlayerNameProperty, self).validate(value) if value is not None and not isinstance(value, self.data_type): raise db.BadValueError('Property %s must be a %s.' % (self.name, self.data_type.__name__)) return value
And here is how you would use the new property declaration:
class Player(db.Model): ...