我正在将应用程序从 Datastore 转换为 ndb,但在 xml 导入例程中遇到了问题。问题是我无法以编程方式确定 ndb.model 类的属性是否为多值属性。
我怀疑这是由于缺乏基本的 Python 技能,因为到目前为止我编写的代码显示该值是“可见的”。因此我无法捕获它。请帮忙。
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
特别是,要获取模型的属性,您应该使用 House._properties,而不是 Nick 提议的代码。 (事实上 , __dict__
可能会在未来的 NDB 版本中被替换为插槽。)
关于python - 如何以编程方式确定 ndb 属性是否为多值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10690418/