python - 如何以编程方式确定 ndb 属性是否为多值

标签 python google-app-engine app-engine-ndb

我正在将应用程序从 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/

相关文章:

python - 如何使用子进程有效地测试基于 readline 的 Python 程序?

google-app-engine - 初始化和销毁​​函数

python - 这个 App Engine Ndb 语法是如何工作的?

python - ndb.query.count() 失败,大型实体的查询期限为 60 秒

python - 使用看门狗库进行文件监控

python - 并行代码在 Python 中的运行速度比在 Matlab 中慢得多

python - 实际上,HRD 中的 "eventual consistency"最终是多少?

python - appengine ndb 按种类名称初始化模型

python - 如何将输出重定向到客户端

python - 使用键(字符串)作为方法的参数