使用 ndb KeyProperty 获取连接实体的列表
Getting list of connected entities using ndb KeyProperty
为 Python 使用 Google App Engine。似乎当您在旧数据库模型下连接数据存储实体时,您可以很容易地获得连接实体的列表,但我似乎无法用 ndb 做同样的事情。
我有:
class User(ndb.Model):
username = ndb.StringProperty()
class Collection(ndb.Model):
owner = ndb.KeyProperty(User)
name = ndb.StringProperty()
photos = ndb.StringProperty(repeated=True)
因此每个 collection 都是由所有者创建的。我以为我可以通过以下方式获得用户的 collections:
collections = user.collections
但这会导致:
collections = user.collections
AttributeError: 'User' object has no attribute 'collections'
根据文档,您似乎可以通过指定 collection_name 关键字在 db 上执行此操作,但我没有看到等效的 ndb。
ndb 不创建反向引用。但是向前做查询同样容易:
Collection.query(Collection.user==user.key)
为 Python 使用 Google App Engine。似乎当您在旧数据库模型下连接数据存储实体时,您可以很容易地获得连接实体的列表,但我似乎无法用 ndb 做同样的事情。
我有:
class User(ndb.Model):
username = ndb.StringProperty()
class Collection(ndb.Model):
owner = ndb.KeyProperty(User)
name = ndb.StringProperty()
photos = ndb.StringProperty(repeated=True)
因此每个 collection 都是由所有者创建的。我以为我可以通过以下方式获得用户的 collections:
collections = user.collections
但这会导致:
collections = user.collections
AttributeError: 'User' object has no attribute 'collections'
根据文档,您似乎可以通过指定 collection_name 关键字在 db 上执行此操作,但我没有看到等效的 ndb。
ndb 不创建反向引用。但是向前做查询同样容易:
Collection.query(Collection.user==user.key)