0

我正在尝试将我的应用程序从旧的 App Engine db 迁移到 ndb。事情进展顺利,直到我开始收到与我的自定义 Decimal 属性相关的奇怪错误。在旧数据库中,我使用了 Nick Johnson 的代码 (http://goo.gl/fpKNs)。我对与 ndb 兼容的函数名称进行了一些更改,但大部分仍然存在。这是我的修改版本:

class DecimalProperty(db.Property):
    def _validate(self, value):
        if not isinstance(value, (Decimal, str)):
            raise datastore_errors.BadValueError('Expected decimal or string, got %r' % (value,))

        return Decimal(value)

    def _db_set_value(self, v, unused_p, value):
        if not isinstance(value, (str, Decimal)):
            raise TypeError('DecimalProperty %s can only be set to string values; received %r' % (self._name, value))
        v.set_stringvalue(str(value))

    def _db_get_value(self, v, unused_p):
        if not v.has_stringvalue():
            try:
                return Decimal(v)
            except ValueError:
                return None

        return Decimal(v.stringvalue())

这是一个示例回溯:

Traceback (most recent call last):
  File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2\webapp2.py", line 1536, in __call__
    rv = self.handle_exception(request, response, e)
  File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2\webapp2.py", line 1530, in __call__
    rv = self.router.dispatch(request, response)
  File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2\webapp2.py", line 1278, in default_dispatcher
    return route.handler_adapter(request, response)
  File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2\webapp2.py", line 1102, in __call__
    return handler.dispatch()
  File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2\webapp2.py", line 572, in dispatch
    return self.handle_exception(e, self.app.debug)
  File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2\webapp2.py", line 570, in dispatch
    return method(*args, **kwargs)
  File "U:\Hefner\Dropbox\Public\Projects\GHI\dev\rpc.py", line 68, in get
    result = func(*args)
  File "U:\Hefner\Dropbox\Public\Projects\GHI\dev\rpc.py", line 278, in getDonations
    response = utilities.getAllDonations(self, settings_key, query_cursor)
  File "U:\Hefner\Dropbox\Public\Projects\GHI\dev\GlobalUtilities.py", line 361, in getAllDonations
    query = models.Donation.gql("WHERE settings_key = :s ORDER BY time_created DESC", s=settings_key)
  File "C:\Program Files (x86)\Google\google_appengine\google\appengine\ext\ndb\model.py", line 2885, in _gql
    *args, **kwds)
  File "C:\Program Files (x86)\Google\google_appengine\google\appengine\ext\ndb\query.py", line 1318, in gql
    qry = _gql(query_string)
  File "C:\Program Files (x86)\Google\google_appengine\google\appengine\ext\ndb\utils.py", line 136, in positional_wrapper
    return wrapped(*args, **kwds)
  File "C:\Program Files (x86)\Google\google_appengine\google\appengine\ext\ndb\query.py", line 1366, in _gql
    if prop._name != name:
AttributeError: 'str' object has no attribute '_name'

我可能错了,但似乎十进制属性返回的是字符串而不是属性的实例,我不知道该怎么做。当我在我的模型类中注释掉时amount_donated = DecimalProperty(),一切正常。

4

3 回答 3

2

看起来您粘贴在 ld db 代码中...

无论如何,请参阅这些文档:https ://developers.google.com/appengine/docs/python/ndb/subclassprop

您想使用 _to_base_type() 和 _from_base_type()。

祝你好运!

于 2012-04-06T05:27:34.997 回答
1

这就是我用于 DecimalProperty 的东西,用于存储货币,物有所值。

class DecimalProperty(ndb.IntegerProperty):
    # Decimal property ideal to store currency values, such as $20.34 
    # See https://developers.google.com/appengine/docs/python/ndb/subclassprop
    def _validate(self, value):
        if not isinstance(value, (Decimal, str, unicode, int, long)):
            raise TypeError('Expected a Decimal, str, unicode, int or long an got instead %s' % repr(value))

    def _to_base_type(self, value):
        return int(Decimal(value) * 100)

    def _from_base_type(self, value):
        return Decimal(value)/Decimal(100) 
于 2013-01-15T08:41:42.400 回答
1

bustrofedon 给出的答案包含舍入问题和错误。这是 a 的更好实现CurrencyProperty

class CurrencyProperty(ndb.IntegerProperty):
    def _validate(self, value):
        if not isinstance(value, (Decimal, float, str, unicode, int, long)):
            raise TypeError("value can't be converted to a Decimal.")

    def _to_base_type(self, value):
        return int(round(Decimal(value) * 100))

    def _from_base_type(self, value):
        return Decimal(value) / 100
于 2014-03-09T17:00:13.450 回答