【问题标题】:Debugging GQL Queries调试 GQL 查询
【发布时间】:2012-05-03 15:24:07
【问题描述】:

我一直在尝试运行这个查询:

my_post = db.GqlQuery("SELECT * FROM Post where __key__ = KEY('Post', 1)")
self.render("showpost.html", my_post = my_post)

我的模板showpost.html 看起来像这样:

<div class="post">
  <h3 class="post-subject">{{ my_post.subject }}</h3>
  <div class="post-content">{{ my_post.content }}</div>
</div

我没有得到id=1 的帖子的详细信息,而是得到一个空白页。尽管存在 id=1 的帖子,但我不确定如何确认 GQLquery 是否确实返回了某些内容。有没有办法检查? 尝试以下方法:

my_post = db.GqlQuery("SELECT * FROM Post where subject = 'hello_world')
self.render("showpost.html", my_post = my_post)

我遇到了同样的问题(空白页),即使有帖子的主题是hello_world

非常感谢

【问题讨论】:

    标签: google-app-engine gql


    【解决方案1】:

    GqlQuery is a class.
    您的代码已创建 GqlQuery 类的实例。
    要从数据存储中返回某些内容,您需要使用以下任一实例方法:

    .fetch() # requires number of entities as first argument
    .get()
    

    例子:

    my_query = db.GqlQuery("SELECT * FROM Post WHERE subject = :subject", 
                           subject="hello_world")
    my_post = my_query.get() # the magic happens here
    self.render("showpost.html", my_post = my_post)
    

    【讨论】:

      【解决方案2】:

      Bernie 是对的,您需要使用 .fetch.get 从您创建的 GqlQuery 中实际检索结果,但对于第一个版本,您有一个键或一个 ID,您不希望完全创建一个查询。而是使用为直接使用它们而提供的方法。

      如果您有 id,请使用:

      my_post = Post.get_by_id(id)
      

      这将比使用查询对象快得多。

      如果您有完整的密钥,请使用:

      my_post = Post.get(key)
      

      这是从数据库中取出实体的最快、最有效的方法。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-02-10
        • 2011-04-08
        • 2010-12-20
        • 1970-01-01
        • 2012-08-15
        • 2011-08-17
        • 2017-06-20
        • 1970-01-01
        相关资源
        最近更新 更多