【发布时间】:2017-12-11 09:33:37
【问题描述】:
我正在使用 ndb 数据存储在 Google App Engine、Python 2.7、标准环境中工作。我正在对我的一些 NDB 模型进行一些更新,其中一个更新要求我向我的“餐厅”模型添加一个新属性(一个浮点数)。过去,当我需要向模型添加属性时,我一直很喜欢 NDB 如何让您在定义模型时指定默认值,如下所示:
class Restaurant(ndb.Model):
... some properties ...
resto_fullresyncstamp = ndb.FloatProperty(default=0.0)
我对这个默认值的理解是,当从数据存储中获取实体时,如果给定实体没有 resto_fullresyncstamp 属性,则会将其添加到模型的本地副本中,并使用默认值。这样一来,我就不必继续检查是否所有内容都为 None,并且我可以假设该属性在我的代码中总是有一个值。所以我应该能够做这样的事情......
lastsync = ...
resto = Restaurant.query()....get()
if not resto:
logging.warning("Failed to get restauraunt: %s" % username)
self.error(404)
return
if lastsync != 0.0 and resto.resto_fullresyncstamp > lastsync:
logging.warning("Requiring client to perform full resynchronization")
self.error(510)
return
...并期望即使“resto_fullresyncstamp”是我模型中的一个新属性,并且特定餐厅从未在任何地方明确设置过该属性,我仍然会得到一个有效的 (0.0) 值来比较.
我实际上得到的是这个错误消息:
Traceback (most recent call last):
....
File "/base/data/home/apps/s~virt-som/1.406133528491612487/virtsom.py", line 5399, in post
if lastsync != 0.0 and resto.resto_fullresyncstamp > lastsync:
AttributeError: 'Restaurant' object has no attribute 'resto_fullresyncstamp'
这不应该吗?我已经使用 NDB 多年了,之前我已经成功地在我的模型中使用过“默认”参数。我错过了什么吗?
【问题讨论】:
标签: python google-app-engine default-value app-engine-ndb