GAE如何打印存储在ndb.LocalStructuredProperty中的项目 [英] GAE How to print items stored in ndb.LocalStructuredProperty

查看:65
本文介绍了GAE如何打印存储在ndb.LocalStructuredProperty中的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Using the example from the Docs but using 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')])

实际上,这就是我在模型中尝试的方式:

This is actually how I am trying it with my model:

guido = Contact(name='Guido')
a = Address()
a.type='home'
a.city='Amsterdam' # etc etc..
guido.addresses.append(a)
guido.put()

现在说明天我要在地址中打印出项目,我从网页的URL中传递了一个contact_id,然后在处理程序中尝试以下操作:

Now say tomorrow I want to print out the items in addresses, I have an contact_id passed in a url from a web page and I try the following in a handler:

    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中,并且我尝试了各种尝试访问项目的方式.我最终希望能够添加,编辑和减去项目,仅出于记录目的,我正在使用此属性,因为它有望在计费中变得便宜"-但直到我启动并运行我的应用程序之后,我才能对其进行测试.我知道我在这里缺少一些基本的知识,感谢您的帮助.

I have looked at most of the SO ndb questions but cannot see anything that answers this. I read in docs that data is stored in opaque blob, and i have tried various ways of trying to access the items. I ultimately want to be able to add, edit and subtract items, Just for the record I am using this property as it hopefully will be "cheaper" in billing - but i can't test it until I have my app up and running. I know I am missing something basic here, Any help appreciated.

推荐答案

看来问题是我有guido.put而不是guido.put()当您检查时,一切看起来都很好-然后将其保存在内存中.但是当您尝试在稍后,您将只获得contact.addresses [[]].一个小错误浪费了我的时间.

It appears the problem was that I had guido.put instead of guido.put() Everything looks ok when you inspect - its in memory then. But when you try and retrieve at a later point, you will only get contact.addresses[[]]. A small mistake that has wasted my time.

这篇关于GAE如何打印存储在ndb.LocalStructuredProperty中的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆