查询嵌套的 KeyProperties
Querying nested KeyProperties
我有以下型号:
class Company(ndb.Model):
name = ndb.StringProperty(indexed=False)
# some other fields
class User(polymodel.PolyModel):
company = ndb.KeyProperty(kind=Company)
# some other fields
class Object(ndb.Model):
user = ndb.KeyProperty(kind=User)
# some other fields
现在我有一个 user
,我想查询与同一公司的其他 Users
关联的 Objects
,如下所示:
Object.query(Object.user.company == user.company)
当然,这行不通,因为 Object.user
是一个密钥,我无法访问除此之外的任何内容。
有什么办法吗?我只需要公司密钥,我在考虑 ComputedProperty
但我不确定这是否是最佳解决方案。另外,最好根据company
.
中的任意字段进行查询
您需要反规范化并存储冗余信息,因为数据存储不支持联接。
例如给定你上面的模型,一个用户只能是一个公司的成员,如果你真的需要搜索其用户是特定公司成员的所有对象,然后将公司密钥存储在对象中。
如果最适合您,请使用计算的 属性。
或者使用始终将 User 作为参数的工厂并以此方式构造 Object。
我有以下型号:
class Company(ndb.Model):
name = ndb.StringProperty(indexed=False)
# some other fields
class User(polymodel.PolyModel):
company = ndb.KeyProperty(kind=Company)
# some other fields
class Object(ndb.Model):
user = ndb.KeyProperty(kind=User)
# some other fields
现在我有一个 user
,我想查询与同一公司的其他 Users
关联的 Objects
,如下所示:
Object.query(Object.user.company == user.company)
当然,这行不通,因为 Object.user
是一个密钥,我无法访问除此之外的任何内容。
有什么办法吗?我只需要公司密钥,我在考虑 ComputedProperty
但我不确定这是否是最佳解决方案。另外,最好根据company
.
您需要反规范化并存储冗余信息,因为数据存储不支持联接。
例如给定你上面的模型,一个用户只能是一个公司的成员,如果你真的需要搜索其用户是特定公司成员的所有对象,然后将公司密钥存储在对象中。
如果最适合您,请使用计算的 属性。
或者使用始终将 User 作为参数的工厂并以此方式构造 Object。