问题:
当我存储数据与bulkloader方法DecimalProperty。get_value_for_datastore从来没有被调用过,当我在数据存储中存储5.4时,就像这样保存5.4;这应该通过get_value_for_datastore传递,并转换为这样的整数:54000;因为我需要4位小数:5.4 * 10000 = 54000;然后返回54000/10000 = 5.4
我有一个模型:
from google.appengine.ext import db
from gae.properties.decimal_property import DecimalProperty
Articles:
price = Decimal(4)
十进制类
from decimal import Decimal
from google.appengine.ext import db
class DecimalProperty(db.Property):
"""
Allows Python's decimal.Decimal type to be stored in the datastore as an
integer. Takes care of putting the decimal point in the right place.
"""
data_type = Decimal
def __init__(self, dec_places, verbose_name=None, name=None, default=None,
required=False, validator=None, choices=None, indexed=True):
super(DecimalProperty, self).__init__(verbose_name, name, default,
required, validator, choices, indexed)
self.__quantize_exp = Decimal('10') ** -dec_places
self.__store_mul = Decimal('10') ** dec_places
def get_value_for_datastore(self, model_inst):
dec = super(DecimalProperty, self).get_value_for_datastore(model_inst)
if dec is None:
return None
dec = dec.quantize(self.__quantize_exp)
return int(dec * self.__store_mul)
def make_value_from_datastore(self, value):
if value is None:
return None
return Decimal(value) / self.__store_mul
def validate(self, value):
if value is not None and not isinstance(value, Decimal):
raise db.BadValueError("Property %s must be a Decimal or string." % self.name)
return super(DecimalProperty, self).validate(value)
def empty(self, value):
return (value is None)
我的bulkloader是:
from google.appengine.ext import bulkload
from google.appengine.api import datastore_types
import datetime
class ArticleLoader(bulkload.Loader):
def __init__(self):
bulkload.Loader.__init__(self, 'Article', [
('price', str)
])
if __name__ == '__main__':
bulkload.main(ArticleLoader())
我的csv文件是这样的
5.4
指出:
如果我输入bulkloader Decimal:
from google.appengine.ext import bulkload
from google.appengine.api import datastore_types
import datetime
from decimal import Decimal
class ArticleLoader(bulkload.Loader):
def __init__(self):
bulkload.Loader.__init__(self, 'Article', [
('price', Decimal)
])
if __name__ == '__main__':
bulkload.main(ArticleLoader())
我得到这个错误:
Loading from line 1...error:
Traceback (most recent call last):
File "C:google_appenginegoogleappengineextbulkloadbulkload_deprecated.py", line 306, in LoadEntities
new_entities = loader.CreateEntity(columns, key_name=key_name)
File "C:google_appenginegoogleappengineextbulkloadbulkload_deprecated.py", line 160, in CreateEntity
entity[name] = converter(val)
File "C:google_appenginegoogleappengineapidatastore.py", line 881, in __setitem__
datastore_types.ValidateProperty(name, value)
File "C:google_appenginegoogleappengineapidatastore_types.py", line 1477, in ValidateProperty
'Unsupported type for property %s: %s' % (name, v.__class__))
BadValueError: Unsupported type for property price: <class 'decimal.Decimal'>
VERY DIRTY SOLUTION:
('price', to_decimal(4))
def to_decimal(dec_places):
def converter(s):
val = int(round(Decimal(s), dec_places) * 10 ** dec_places)
return val
return converter
当您使用bulkloader批量加载数据时,您的模型定义不会被加载或使用-数据使用低级API直接加载到数据存储中。因此,也不会调用任何模型代码。你的"非常肮脏的解决方案"是正确的方法。