【问题标题】:How do I get the parent entity in GAE?如何在 GAE 中获取父实体?
【发布时间】:2014-05-27 22:30:02
【问题描述】:

我通过以下行创建了一个“发布”实体:

    post = Post(date=datetime.now(),title=postInfo['title'],body=postInfo['body'],postid=postIdForTitle(postInfo['title']),parent=author)

其中author 是一个数据存储实体。

如何访问“帖子”的父级?我尝试做post.parent,但显然“父”是一个函数。我在文档中找不到这个。

【问题讨论】:

    标签: python google-app-engine google-cloud-datastore webapp2


    【解决方案1】:

    您可以从子实体的密钥中获取父实体的密钥。看关键类:

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

    【讨论】:

    • 非常感谢您的回答。我如何使用密钥进行查询?编辑:嗯,我什至不必查询密钥来获取实体,它就可以工作......这怎么可能?
    【解决方案2】:

    试试这个:

    key_name = postIdForTitle(postInfo['title'])
    parent = post.get_by_key_name(key_name).parent
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-27
      • 2014-06-03
      • 1970-01-01
      • 2019-08-01
      • 1970-01-01
      • 2023-03-16
      相关资源
      最近更新 更多