如何检索和显示 GQL 查询返回的多个结果的键?
main.py
class MainPage(webapp2.RequestHandler):
def get(self):
author = "William Shakespeare"
q = db.GqlQuery("SELECT * FROM Book " +
"WHERE author = :1 ", author)
keys = q.Key
template_values = {
'author': author,
'key': key,
}
template = jinja_environment.get_template('index.html')
self.response.out.write(template.render(template_values))
更新 1:
当我使用时
keys = q.key()
我收到此错误:
File "C:\programming_google_app_engine_repo\datastore\main.py", line 17, in get key = q.key() AttributeError: 'GqlQuery' object has no attribute 'key'
最佳答案
您的代码存在多个问题。请退后一步,思考一下您想要做什么,并再次浏览一下文档。
问题1
GqlQuery
的结果没有键。您需要使用 .get()
或 .fetch(1)
来访问 Book
对象。
q = db.GqlQuery("SELECT * FROM Book WHERE author = :1 ", author)
my_book = q.get()
问题2
要获取方法
的值,您需要记住包含括号。
my_book_key = my_book.key() # don't forget the parens/brackets
template_values = {
'author': author,
'key': my_book_key,
}
关于python - 属性错误: 'GqlQuery' object has no attribute 'Key' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15579873/