【发布时间】:2014-03-04 22:52:29
【问题描述】:
我有一个 HUser 模型(源自 Google 的 User 类),它又包含 0 到 n 个社交帐户实例。这些帐户可以是对 Facebook、Twitter 或 LinkedIn 帐户的引用。我已经建立了一个Account 模型,并在我的User 模型中定义了一个StructuredProperty repeated=True,如下所示:
class Account(ndb.Expando):
account_type = ndb.StringProperty(required=True, choices=['fb', 'tw', 'li'])
account_id = ndb.StringProperty()
access_token = ndb.StringProperty()
...
class HUser(User):
email = ndb.StringProperty(required=True, validator=validate_email)
created = ndb.DateTimeProperty(auto_now_add=True)
accounts = ndb.StructuredProperty(Account, repeated=True)
如果我只向我的用户添加 Facebook 或 LinkedIn 帐户,一切都会按预期进行。但奇怪的是,每当我添加 Twitter 帐户时,我添加到同一用户的所有后续帐户都存储为_BaseValue(Account()),而不是直接存储为Account()。因此,在我尝试获取帐户的页面中,我通常会收到如下错误:
AttributeError: '_BaseValue' 对象没有属性 'account_type'
我已经读到这些 _BaseValue 转换是 Google 的 ndb 源代码中的一个错误,但我该如何摆脱它呢?目前我正在使用这种糟糕的解决方法来绕过异常:
if type(account) == _BaseValue:
account = account.b_val
logging.warn("WARN: %s account %s was of type _BaseValue..." % (account.account_type, account.account_id))
感谢您的帮助!
【问题讨论】:
标签: python google-app-engine google-cloud-datastore app-engine-ndb