google-app-engine - 如何在 ndb 重复结构化属性中查找实例

标签 google-app-engine data-structures python-2.7 app-engine-ndb

当我有一个 NDB 重复属性时,我必须遍历列表以找到我正在寻找的属性。当我查看数据存储区时,结构化属性的每个属性都是一个列表。我以为我可以使用 Python 列表索引方法,但这不起作用。

那么有没有一种简单的方法可以找到我需要的结构化属性实例。

class Instance(ndb.Model)
    instance_id = ndb.StringProperty()
    instance_data = ndb.TextProperty()

class Example(ndb.Model):
    instances = ndb.StructuredProperty(Instance, repeated = True)

我试过:

instance_id = 'thisone'
index = entity.instances.instance_id.index(instance_id)
data =  entity.instances.instance_data[index]

但我不得不:

instance_id = 'thisone'
for each in entity.instances :
    if each.instance_id = instance_id :
        instance_data = each.instance_data
        break

最佳答案

所以这就是问题所在 - entity.instances 是一个 python 列表,根据 documentation here .你想做的更像是一个命令。 Guido 在 Google Group 中拒绝了一项功能请求,这似乎与您的要求相同:https://groups.google.com/d/topic/appengine-ndb-discuss/Kj7Ymhr5hlE/discussion

entity.instances[0] 返回该列表中的第一个实例。所有其他列表操作似乎也有效。但是您正在尝试有效地查询该列表。

如果您已经知道 ID 是什么并且您的列表很大,您始终可以将该 ID 设置为键并调用数据存储以获取该实例。当您创建实例时,请执行以下操作:

new_instance = Instance(id=instance_id, instance_data=some_data)

然后像这样加载它:

existing_instance = Instance.get_by_id(instance_id)

关于google-app-engine - 如何在 ndb 重复结构化属性中查找实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14243798/

相关文章:

python-2.7 - 如何在 VScode 中正确设置 PYTHONPATH env

python-2.7 - pandas.read_hdf 出错

eclipse - 无法为 eclipse 3.7 安装谷歌插件

algorithm - 我该如何解决这个算法问题。最佳时间和空间复杂度是多少?

algorithm - 复制带有 next 和随机指针的链表,只给链表上的读权限

java - 确定要使用的 Java 集合类型

python - TemplateDoesNotExist 但它存在

google-app-engine - 谷歌云端点 : Return type of api method

java - 在 Google App Engine 中使用 Java,存储和访问大型静态数据的最佳方式是什么?

google-app-engine - 使用 google appengine maven 插件部署 Jax-RS war