我正在尝试将我的应用程序从旧的 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()
,一切正常。