Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/google-app-engine/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google app engine Google应用程序引擎:无键名称属性_Google App Engine_Google Cloud Datastore - Fatal编程技术网

Google app engine Google应用程序引擎:无键名称属性

Google app engine Google应用程序引擎:无键名称属性,google-app-engine,google-cloud-datastore,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(webap

我试图了解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模型试过了,两种方法都不行

提前感谢您的回答

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


请阅读
ndb
模型类的文档。特别是在模型构造函数参数上


您将看到它需要
id
,而不是
,而不是key\u name
key\u name
db
api的构造函数参数。

我有一个类似的解决方案,我想要实体的key\u名称(只有key\u name,没有ID)。我能够从属性
self.key().name()

中找到密钥名称,您发布的代码运行良好。一定有您没有共享的内容。请使用id代替key_name。请参阅此问题的答案:谢谢,它现在可以使用了。我买了一本关于GAE的书,主要是关于旧的“db”模块api(o'reilly版)。。。由于我在官方文件中找不到任何真实的例子,我有点回到了以前的一个例子,在那里这种功能性被真正地记录下来。我在同一本书的同一个示例代码中遇到了同样的问题。
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()