如何在 appengine python 中查询没有祖先的实体

How to query entities without no ancestors in appengine python

考虑以下代码片段:

from google.appengine.ext import ndb
import pprint

class Entity(ndb.Model):
     name = ndb.StringProperty()
     age = ndb.IntegerProperty()

entity = Entity()
entity.name = "hello"
entity.age = 23
entity.key = ndb.Key(Entity, "p1")
entity.put()

e2 = Entity()
e2.key = ndb.Key(Entity, "p2", parent=ndb.Key(Entity, "p1"))
e2.name = "he11o2"
e2.age = 34
e2.put()

我想查询实体 table 没有任何记录 parent与之关联。对于上面的例子,它应该只产生 p1 实体。

我怎样才能做到这一点?

你不能。您只能查询索引中存在的内容。除非明确设置为 None(并且你不能为 parents 这样做),否则没有价值的东西不能被查询。

我可以建议的唯一方法是计算 属性 或设置为 None 的其他 属性 如果没有 parent 或 parent 键或标志。然后您可以查询所有具有 parent=None 的实体。 parent 是实体的 属性。