0

我一直在尝试运行此查询:

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

非常感谢

4

2 回答 2

2

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

如果您有 id,请使用:

my_post = Post.get_by_id(id)

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

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

my_post = Post.get(key)

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

于 2012-05-03T17:17:56.690 回答
2

GqlQuery是一类
您的代码已经创建了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)
于 2012-05-03T15:30:19.717 回答