AppEngine NDB PolyModel 获取属性 [英] AppEngine NDB PolyModel getting properties
问题描述
我在使用 appengine 时遇到了一个我似乎无法解决的问题:
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()
因此,使用上面的类,当我查询所有 ItemInHouse,然后尝试访问那些具有 MedicalItem 的 iteminhouse 时,我无法获取 med_sub_type.即:
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)
在 itm.item.med_sub_type 处引发错误.我什至尝试过:itm.item._values["med_sub_type"].b_val
但这仍然抛出一个 AttributeError: 'Item' object has no attribute 'med_sub_type'.我确实在 class_
属性中看到它具有 Item
和 MedicalItem
属性,但我无法访问它.有什么想法吗?
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?
谢谢乔恩
推荐答案
我担心您尝试执行的操作可能无法实现——我认为我没有预料到将 PolyModel 实例存储为 StructuredProperty 值.这能解释你所看到的吗?您可能想向 NDB 项目跟踪器提交功能请求,但我不能保证它会被实施.
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.
这篇关于AppEngine NDB PolyModel 获取属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!