AppEngine NDB PolyModel getting properties

不羁的心 提交于 2019-12-12 01:34:49

问题


I am having a problem with appengine that I can't seem to figure out:

from google.appengine.ext import ndb
from google.appengine.ext.ndb import polymodel

class Item(polymodel.PolyModel):
      name = ndb.StringProperty()
      type = ndb.StringProperty(choices=["Medical","Food"])
      sub_category_type = ndb.StringProperty()
      sub_category_sub_type = ndb.StringProperty()

class MedicalItem(Item):
      med_sub_type = ndb.StringProperty()
      can_split_item = ndb.BooleanProperty()

class ItemInHouse(ndb.Model):
      item = ndb.StructuredProperty(Item)
      amount_of_item = ndb.FloatProperty()

So using the classes above, when I query for all ItemInHouse, and then I try to access those iteminhouse that have a MedicalItem, I am unable to get med_sub_type. That is:

itms = ItemInHouse.query(ItemInHouse.item.type == "Medical").fetch()
for itm in itms:
    self.response.out.write(itm.item.med_sub_type)

Throws an error at the itm.item.med_sub_type. I have even tried: itm.item._values["med_sub_type"].b_val but this still throws an AttributeError: 'Item' object has no attribute 'med_sub_type'. I do see in the class_ property it has Item and MedicalItem properties, but I am unable to acces it. Any ideas?

Thanks Jon


回答1:


I fear that what you are trying to do may not be possible -- I don't think I had anticipated storing PolyModel instances as StructuredProperty values. Does that explain what you are seeing? You may want to file a feature request at the NDB item tracker but I can't promise it will be implemented.



来源:https://stackoverflow.com/questions/14419247/appengine-ndb-polymodel-getting-properties

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