如何在 Google App Engine (Python) NDB/Datastore 中从子实体查询父实体? [英] How to query parent entity from child entity in Google App Engine (Python) NDB/Datastore?

查看:35
本文介绍了如何在 Google App Engine (Python) NDB/Datastore 中从子实体查询父实体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

My question is very fundamental, I want to know straight forward and right way to access attribute values of parent entity from a child in App Engine Python. For example I have following model schema. I am using Python 2.7 and 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 模型作为 StructuredProperty.我大部分时间都基于画廊以外的其他标志显示所有图像中的图像(一种情况是从所有图像生成幻灯片,如果 is_slider="yes".所以直接从图像查询)但仍然想显示相关画廊的信息为什么我想知道如何访问父数据.

Here "Gallery" is parent of "Image". They form an entity group Exhibition=>Gallery=>Image. I want to display images from Image model along with gallery detail they belong. I can access child entity from a parent (Image from Gallery) but not vice versa. I don't want to use Image model as StructuredProperty in Gallery model. I am displaying images most of the time from all images based on other flags than gallery (one situation is generating a slideshow from all images if is_slider="yes". so querying from Image directly) but still want to display info of related gallery that's why I want to know how to access parent data.

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

I feel this is a very generic problem and looking for a simple solution like direct access to parent than going back to top of entity group and query Gallery model with some complex logic. Any help is appreciated.

推荐答案

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

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

这篇关于如何在 Google App Engine (Python) NDB/Datastore 中从子实体查询父实体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆