我一直在尝试运行以下查询:
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
非常感谢
发布于 2012-05-03 23:30:20
您的代码已经创建了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-04 01:17:57
Bernie是正确的,因为您需要使用.fetch或.get从您创建的GqlQuery中实际检索结果,但是对于第一个版本,您有一个键或id,您根本不想创建一个查询。相反,应该使用为直接使用它们而提供的方法。
如果你有一个id,使用:
my_post = Post.get_by_id(id)这将比使用查询对象快得多。
如果您有一个完整的密钥,请使用:
my_post = Post.get(key)这是从数据库中取出实体的最快、最有效的方法。
https://stackoverflow.com/questions/10434459
复制相似问题