有没有办法用get_multi忽略无效键?

时间:2013-12-08 02:47:19

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

对于包含无效ID的密钥,我希望get_multi返回None

class Entity(ndb.Model):
    pass

ids = [0, 1]
rows = ndb.get_multi([Key(Entity, id) for id in ids])

实际结果

BadRequestError('missing key id/name')

期望的结果

[None, <Entity with id 1>]

我没有在filter上调用ids的原因是我希望ids中的索引与rows中的索引相对应。

1 个答案:

答案 0 :(得分:2)

由于get_multi只是一个辅助函数,到目前为止我最好的解决方案是复制代码,只运行查询id > 0

ids = [0, 1]
futures = [Key(Entity, id).get_async() if id > 0 else None for id in ids]
rows = [future.get_result() if future else None for future in futures]