如何以编程方式确定 ndb 属性是否为多值 [英] How to programmatically determine if ndb property is multivalue
问题描述
我正在将一个应用程序从 Datastore 转换为 ndb,并且在 xml 导入例程中遇到了问题.问题是我无法以编程方式确定 ndb.model 类的属性是否为多值属性.
I am translating an app from Datastore to ndb and have encountered a problem in the xml import routine. The problem is that I am not able to programmatically determine whether a property of a ndb.model class is a multivalue property or not.
我怀疑这是由于缺乏基本的 Python 技能,因为到目前为止我提出的代码表明该值是可见的".因此我无法抓住它.请帮忙.
I suspect that this is due to lack of basic Python skills since the code I have come up with so far shows that the value is "visible". I am thus not able to grab it. Please help.
from google.appengine.ext import ndb
class House(ndb.Model):
name = ndb.StringProperty()
rooms = ndb.StringProperty(repeated=True)
print 'Properties:'
for p in House._properties:
print getattr(House,p)
print '\nRepeated:'
for p in House._properties:
print getattr(getattr(House,p),'repeated',None)
结果如下:
Properties:
StringProperty('rooms', repeated=True)
StringProperty('name')
Repeated:
None
None
推荐答案
实际上,下划线前缀的选项是官方 API.这是他们没有记录的错误,我会修复它.我在这里解释了一点:http://code.google.com/p/appengine-ndb-experiment/issues/detail?id=187
Actually, the underscore-prefixed options are the official API. It's a bug that they aren't documented, I will fix that. I explained a little more here: http://code.google.com/p/appengine-ndb-experiment/issues/detail?id=187
特别是,要获取模型的属性,您应该使用 House._properties,而不是 Nick 提出的代码.(事实上,__dict__
可能会在未来的 NDB 版本中被插槽替换.)
In particular, to get the properties of a model, you should use House._properties, not the code that Nick proposed. (In fact, __dict__
may be replaced with slots in future NDB versions.)
这篇关于如何以编程方式确定 ndb 属性是否为多值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!