python - 属性错误: 'GqlQuery' object has no attribute 'Key'

标签 python google-app-engine python-2.7 jinja2

如何检索和显示 GQL 查询返回的多个结果的键?

ma​​in.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/

相关文章:

python - PyQt5:Gtk-CRITICAL **:IA__gtk_widget_style_get:断言 'GTK_IS_WIDGET (widget)' 失败

file - 谷歌应用引擎运行可执行文件

python - sqlalchemy 创建不带命名关键字的插入对象

python-2.7 - 如何配置 virtualenvwrapper 与 pyenv 一起使用

python - 使用 Python 2.7.x 连接到 Google Analytics 时出现 403 错误

php - 使用 PHP 检索 MySQL 多个查询并处理

python - 在 Sklearn Pipeline 中组合功能

python - 'exec' 不适用于私有(private)方法 Python

eclipse - 如何在 Eclipse 中激活 Faces 配置编辑器?

python - 如何将 Polymer 项目引入 Google App Engine