Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
283 views
in Technique[技术] by (71.8m points)

python - AppEngine NDB PolyModel getting properties

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...