Joe Gregorio

This is a DecimalProperty class, which will allow you to add Python decimals as properties to your App Engine models. If you want to create your own property types then I strongly suggest reading Rafe's article on Extending Model Properties, which is what I followed when creating DecimalProperty. Just a bit of code I needed for a project. Posted here if others need it:

from google.appengine.ext import db
from decimal import Decimal
class DecimalProperty(db.Property):
  # Tell what the user type is.
  data_type = Decimal
  # For writing to datastore.
  def get_value_for_datastore(self, model_instance):
    value = super(DecimalProperty,
    return unicode(value)
  # For reading from datastore.
  def make_value_from_datastore(self, value):
    if value is None:
      return None
    return Decimal(value)
  def validate(self, value):
    if value is not None and not isinstance(value, Decimal):
      raise BadValueError('Property %s must be convertible '
                          'to a Decimal instance (%s)' %
                          (, value))
    return super(DecimalProperty, self).validate(value)
  def empty(self, value):
    return not value
comments powered by Disqus