【发布时间】:2014-03-04 23:50:25
【问题描述】:
我正在尝试在另一个模型中使用 Expando 模型作为重复的 StructuredProperty。也就是说,我想在我的User 模型中添加不定数量的Accounts。由于 Accounts 可以根据其类型具有不同的属性(Accounts 是对社交网络帐户的引用,例如 Twitter 需要比 Facebook 更多的信息来进行 OAuth 过程),我将我的 Account 模型设计为 @987654328 @。我已在模型定义中添加了所有基本信息,但我计划为特定社交网络添加自定义属性(例如,Twitter 的特定 access_token_secret 属性)。
1/ 你能确认下面的设计(Expando 重复StructuredProperty)应该工作吗?
class Account(ndb.Expando):
account_type = ndb.StringProperty(required=True, choices=['fb', 'tw', 'li'])
account_id = ndb.StringProperty()
state = 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)
2/ 现在我面临的问题是:当我将 Facebook 帐户添加到我的 HUser 实例时,一切正常;但是,当我将 Twitter 帐户附加到同一个实例并添加一个未在模型中声明的新属性时,问题就出现了,如下所示:
for account in huser.accounts:
if account.state == "state_we_re_looking_for" and account.account_type == 'tw':
# we found the appropriate Twitter account reference
account.access_token_secret = "..." # store the access token secret fetched from Twitter API
huser.put() # save to the Datastore
break
这个操作应该将访问令牌秘密保存在我的User 的 Twitter Account 实例中,但实际上它保存在 Facebook Account 实例中(索引为 0 )!
我做错了什么?
谢谢。
【问题讨论】:
标签: python google-app-engine google-cloud-datastore app-engine-ndb