我正在尝试了解 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_name
。 key_name
是 db
api 的构造函数参数。
关于google-app-engine - 谷歌应用引擎 : No key_name attribute,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24332648/