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

查看:31
本文介绍了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天全站免登陆