如何在Google App Engine(Python)NDB / Datastore中从子实体查询父实体? [英] How to query parent entity from child entity in Google App Engine (Python) NDB/Datastore?
问题描述
我的问题非常重要,我想知道从App Engine Python中的子项访问父实体的属性值的简单方法。例如,我有以下模型架构。
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()
这里图库是图像的父亲。他们形成一个实体组展览=>图库=>图像。
我想显示图像模型中的图像以及它们所属的图库细节。我可以从父母访问子实体(图片来自图库),但反之亦然。我不想在图库模型中将图像模型用作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 / code>
Use: image_instance.key.parent().get()
https ://developers.google.com/appengine/docs/python/ndb/keyclass#Key_parent
这篇关于如何在Google App Engine(Python)NDB / Datastore中从子实体查询父实体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!