google-app-engine - 谷歌应用引擎 : No key_name attribute

标签 google-app-engine google-cloud-datastore

我正在尝试了解 GAE 的大部分工作原理,但由于某些奇怪的原因,这段代码输出了一个

AttributeError: 'User_Account' object has no attribute 'key_name'

这是相关的两个代码摘录:

class User_Account(ndb.Model):
    name = ndb.StringProperty()
    firstname = ndb.StringProperty()


class AddUser(webapp2.RequestHandler):

    def get(self):
        test_user = User_Account(name = "snow", firstname ="jon", key_name="jon")

我已经用 db 和 ndb 模型试过了,这两种方法都不行......

提前感谢您的回答。

更新:这是“完整”代码(我删除了所有其他不必要的部分):

import webapp2
import cgi
from google.appengine.ext import ndb


MAIN_PAGE_HTML = """\
<html>
  <body>
    <br/>
    <a href="/add_user"> Add a user </a>
  </body>
</html>
"""

class Comment(ndb.Model):
    content = ndb.StringProperty()

class User_Account(ndb.Model):
    name = ndb.StringProperty()
    firstname = ndb.StringProperty()
    comments = ndb.KeyProperty(repeated=True)


class AddUser(webapp2.RequestHandler):
    def get(self):
        test_user = User_Account(name = "jon", firstname ="snow", key_name="jon")
        self.response.write(test_user.key_name + "<br/>")

        test_user.put()

        self.response.write("User added")

class MainPage(webapp2.RequestHandler):
    def get(self):
        self.response.write(MAIN_PAGE_HTML)


application = webapp2.WSGIApplication([
    ('/', MainPage),
    ('/add_user', AddUser)
], debug=True)

摩尔编辑: 即使是这个非常简单的代码,在开发控制台中执行时也会输出错误

import os
import pprint

from google.appengine.ext import ndb

class Comment(ndb.Model):
    content = ndb.StringProperty()


test_comment = Comment(content="Hello world", key_name="hello")
test_comment.put()

最佳答案

请阅读 ndb 模型类的文档。特别是关于模型构造函数参数。 https://developers.google.com/appengine/docs/python/ndb/modelclass#Constructor

您会看到它采用id,而不是key_namekey_namedb api 的构造函数参数。

关于google-app-engine - 谷歌应用引擎 : No key_name attribute,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24332648/

相关文章:

java - JSP Google API 导入错误

python - 无论我做什么, super 简单的 Flask 应用程序的云构建都失败了

google-app-engine - 列表上的数据存储查询过滤

database - GAE 数据存储的前端 - 类似 phpMyAdmin 的 GAE

python - 如何避免在 Python Google App Engine Ndb 中循环导入(要避免的模式)

python - 如何分析 Google App Engine python27 运行时(不是 python)

java - Google App Engine Connected Android (Eclipse) 设备端点错误

用于 google appengine 访问的 php 包装库

google-cloud-datastore - 如何以流的形式使用对 Google Cloud Datastore 的更改?

google-app-engine - 在迁移到高复制数据存储后,在另一个增量副本之前可以安全地接触数据吗?