New entity in repeated StructuredProperty stored as a _BaseValue

我是研究僧i 提交于 2019-12-24 04:59:24

问题


I have a HUser model (derived from Google's User class) which in turn contains 0 to n instances of social accounts. These accounts can be references to Facebook, Twitter or LinkedIn accounts. I have built an Account model, and defined a StructuredProperty in my User model with repeated=True, like this:

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)

If I only add Facebook or LinkedIn accounts to my user, everything works as expected. But the strange thing is, whenever I add a Twitter account, all subsequent accounts I add to that same user are stored as _BaseValue(Account()), and not Account() directly. So in my pages where I try to fetch accounts, I usually get errors like:

AttributeError: '_BaseValue' object has no attribute 'account_type'

I've read that these _BaseValue conversions are a bug in Google's ndb source code, but how can I get rid of it? Currently I'm using this awful workaround to bypass exceptions:

if type(account) == _BaseValue:
    account = account.b_val
    logging.warn("WARN: %s account %s was of type _BaseValue..." % (account.account_type, account.account_id))

Thanks for your help!


回答1:


How are you accessing your repeated property? ndb models store properties as _BaseValues and convert "seamlessly" (unfortunately not always the case) to your type (called the UserValue in ndb). Because of this, you have to be careful about how you store properties outside of your model. Consider this:

myUser = HUser(...)
accounts = myUser.accounts
myUser.put()
accounts[0] # This is a _BaseValue(Account)
myUser.accounts[0] # This is an Account

This is an open bug on the ndb issue tracker.



来源:https://stackoverflow.com/questions/22184888/new-entity-in-repeated-structuredproperty-stored-as-a-basevalue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!