12

我的问题非常基础,我想知道在 App Engine Python 中从子实体访问父实体属性值的直接和正确方法。例如,我有以下模型架构。我正在使用 Python 2.7 和 NDB。

class Gallery(ndb.Model):
    category    = ndb.StringProperty()
    title       = ndb.StringProperty()
    subtitle    = ndb.StringProperty()

class Image(ndb.Model):
    blob_key    = ndb.BlobKeyProperty()
    title       = ndb.StringProperty()
    gallery     = ndb.StringProperty()
    is_slider   = ndb.StringProperty()

这里“Gallery”是“Image”的父级。它们形成了一个实体组展览=>画廊=>图像。我想显示图像模型中的图像以及它们所属的画廊细节。我可以从父实体(图片来自画廊)访问子实体,但反之则不行。我不想在 Gallery 模型中使用 Image 模型作为 StructuredProperty。我大部分时间都在基于画廊以外的其他标志显示所有图像的图像(一种情况是如果 is_slider="yes" 从所有图像生成幻灯片。所以直接从图像查询)但仍然想显示相关画廊的信息为什么我想知道如何访问父数据。

我觉得这是一个非常普遍的问题,寻找一个简单的解决方案,比如直接访问父级,而不是回到实体组的顶部并使用一些复杂的逻辑查询 Gallery 模型。任何帮助表示赞赏。

4

1 回答 1

31

采用:image_instance.key.parent().get()

https://developers.google.com/appengine/docs/python/ndb/keyclass#Key_parent

于 2012-04-09T00:36:34.787 回答