Google app engine GAE如何打印存储在ndb.LocalStructuredProperty中的项目

Google app engine GAE如何打印存储在ndb.LocalStructuredProperty中的项目,google-app-engine,google-cloud-datastore,app-engine-ndb,Google App Engine,Google Cloud Datastore,App Engine Ndb,使用文档中的示例,但使用LocalStructuredProperty: class Address(ndb.Model): type = ndb.StringProperty() # E.g., 'home', 'work' street = ndb.StringProperty() city = ndb.StringProperty() class Contact(ndb.Model): name = ndb.StringProperty() add

使用文档中的示例,但使用LocalStructuredProperty:

class Address(ndb.Model):
    type = ndb.StringProperty() # E.g., 'home', 'work'
    street = ndb.StringProperty()
    city = ndb.StringProperty()

class Contact(ndb.Model):
    name = ndb.StringProperty()
    addresses = ndb.LocalStructuredProperty(Address, repeated=True)

guido = Contact(name='Guido',
            addresses=[Address(type='home',
                               city='Amsterdam'),
                       Address(type='work',
                               street='Spear St',
                               city='SF')])
编辑: 这就是我在我的模型上尝试的方式:

guido = Contact(name='Guido')
a = Address()
a.type='home'
a.city='Amsterdam' # etc etc..
guido.addresses.append(a)
guido.put()
现在,假设明天我想打印地址中的项目,我从网页中输入了一个联系人id,我在处理程序中尝试以下操作:

    contact_id = self.request.get('contact_id')
    contact = Contact.get_by_id(int(contact_id))
    logging.info("=====start contact addresses report===========================================")
    for item in contact.addresses:
         logging.info(item.type)
         logging.info(item.city)
    logging.info("=====finish contact addresses report===========================================")
我已经看过了大部分的SO ndb问题,但看不到任何答案。 我在文档中读到数据存储在不透明的blob中,我尝试了各种方法来访问这些项目。 我最终希望能够添加、编辑和减去项目,只是为了记录我正在使用的这个属性,因为它希望在计费方面“更便宜”——但在我的应用程序启动并运行之前,我无法测试它。
我知道我这里缺少一些基本的东西,非常感谢您的帮助。

您可以按原样运行以下代码,您将看到存储在
ndb.LocalStructuredProperty
中的项目正在被正确读取:

import webapp2
from google.appengine.ext import ndb

class Address(ndb.Model):
  type = ndb.StringProperty()
  street = ndb.StringProperty()
  city = ndb.StringProperty()

class Contact(ndb.Model):
  name = ndb.StringProperty()
  addresses = ndb.LocalStructuredProperty(Address, repeated=True)

class MainHandler(webapp2.RequestHandler):
  def get(self):
    guido = Contact(name='Guido',
                    addresses=[Address(type='home',
                                       city='Amsterdam'),
                               Address(type='work',
                                       street='Spear St',
                                       city='SF')])
    guido.put()
    self.response.headers['Content-Type'] = 'text/plain'

    self.response.write('name: %s\n' % guido.name)
    for address in guido.addresses:
      self.response.write('  type: %s\n' % address.type)
      self.response.write('  city: %s\n' % address.city)

app = webapp2.WSGIApplication([
    ('/', MainHandler)
  ], debug=True)

因此,很明显,您在实际应用程序中做了一些错误的事情,并且可能没有正确存储实际值。

问题似乎是我使用了guido.put而不是guido.put() 当你检查的时候,一切看起来都很好——它在记忆中。但是当你尝试在 稍后,您将只获得联系人。地址[]。
这是一个浪费我时间的小错误。

尝试删除地址的
[0]
。Hanks Jimmy,我的模型与此明显不同,但结构相似,我似乎无法让它显示数据。也许这就是我在put()之前构建数据的方式谢谢@Lipis,也许在晚上睡觉后我就能解决这个问题。will update.OK,由于我的应用程序的结构,联系人是在地址之前创建的,应用程序使用Contact.get_by_id(int(Contact_id))查询联系人,如我的示例所示,然后使用与上面所示非常类似的表单中的数据添加地址。如果我在guido.put()后面显示地址,使用当前引用联系ie'guido',我可以看到它们,如您所示。当我后来问这个问题时,我似乎看不到地址?我将尝试复制我们在这个简单示例中使用的代码,以便更准确地模拟我的代码结构。我将在稍后进行此操作。