【发布时间】:2015-05-30 00:54:32
【问题描述】:
我正在尝试获取一长串实体,这些实体都引用了几个不同的相关实体之一。 cmets 中对此进行了解释,但基本上许多“项目”都引用了一些“公司”。我不想对unique_key(即key.get())中的每个key 进行多次查询,所以我认为以下方法可行,但它返回一个空列表。祈祷告诉我,我做错了什么?或者有没有更好的方法来完成许多项目引用一些项目的这种关系,同时最大限度地减少对数据库的调用(我是 AppEngine Datastore 的新手)。
注意,这是用 Python 编写的,使用的是应用引擎提供的 ndb 库。
# "items" is a list of entities that have a property "parenty_company"
# parent_company is a string of the Company key
# I get a unique list of all Key strings and convert them to Keys
# I then query for where the Company Key is in my unique list
unique_keys = list(set([ndb.Key(Company, prop.parent_company) for prop in items]))
companies = Company.query(Company.key.IN(unique_keys)).fetch()
【问题讨论】:
-
为什么不使用
ndb.get_multi(unique_keys)?
标签: database google-app-engine google-cloud-datastore app-engine-ndb