How to query parent entity from child entity in Google App Engine (Python) NDB/Datastore?

10,212

Use: image_instance.key.parent().get()

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

Share:
10,212
fusionstrings
Author by

fusionstrings

I'm a frontend and JavaScript full stack web developer with over 14 years of relevant experience. I am working as Frontend Lead in Comeon AB, Stockholm, Sweden. I'm passionate about UX on Distributed web platforms which are both Usable and Accessible without compromising aesthetics. I'm specialized in crafting tasteful Serverless Web Application with truly full-stack experience and vision. I am skilled in bringing ideas to life from the skills and knowledge ranging from Graphic Design, Information Architecture, System Design, UX / UI, Design Systems, Dev Ops, Cloud Tools, SEO and Developer Experience toolings. My ideals are realized by closely following modern development semantics, standards, best practices and creativity, in a pragmatic way. I prefer to work with Serverless architecture using Cloudflare Workers, Google Cloud Run, Svelte, Figma, Web Assembly, Rust and Deno. I am obsessed with optimization for web delivery and that has been my area of exploration lately. Experimenting on Distributed web using Hyperspace, Stellar, IPFS and Inrupt Solid. Future is exciting.

Updated on June 11, 2022

Comments

  • fusionstrings
    fusionstrings almost 2 years

    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()
    

    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.