Google app engine 基于KeyProperty实例的ndb查询模型
我有一个简单的ndb模型,如下所示:Google app engine 基于KeyProperty实例的ndb查询模型,google-app-engine,app-engine-ndb,Google App Engine,App Engine Ndb,我有一个简单的ndb模型,如下所示: class TaskList(ndb.Model): title = ndb.StringProperty() description = ndb.TextProperty() priority = ndb.IntegerProperty() class UserProfile(ndb.Model): user = ndb.UserProperty() tasks = ndb.KeyProperty(TaskLis
class TaskList(ndb.Model):
title = ndb.StringProperty()
description = ndb.TextProperty()
priority = ndb.IntegerProperty()
class UserProfile(ndb.Model):
user = ndb.UserProperty()
tasks = ndb.KeyProperty(TaskList)
task = ndb.Key(TaskList, 7).get()
众所周知,任务列表对象将具有实体种类
实体键
和ID
。
给我一个身份证,说7。
我可以很好地获得ID为7的对象,如下所示:
class TaskList(ndb.Model):
title = ndb.StringProperty()
description = ndb.TextProperty()
priority = ndb.IntegerProperty()
class UserProfile(ndb.Model):
user = ndb.UserProperty()
tasks = ndb.KeyProperty(TaskList)
task = ndb.Key(TaskList, 7).get()
但是如何获得任务ID为7的用户
我试过:
tsk = ndb.Key(TaskList, 7).get()
user = UserProfile.query(UserProfile.tasks == tsk.key)
它可以工作,但是有更好的方法吗?您就快到了-但是不需要获取任务,只需要再次使用它的key属性:
task_key = ndb.Key(TaskList, 7)
user = UserProfile.query(UserProfile.tasks == task_key)
或相当于:
user = UserProfile.query(UserProfile.tasks == ndb.Key(TaskList, 7))
是否存在无法在任务列表实体本身上拥有用户属性的原因?