在GAE中存储字典列表 [英] Store a list of dictionaries in GAE

查看:73
本文介绍了在GAE中存储字典列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个约20个对象的列表,每个对象返回一个10个字典的列表。

我试图为GAE列表中的每个对象存储10个字典的列表;我不认为我正确地编写代码来将这些信息存储到GAE。

这是我的:
在我的主请求处理程序之前,我有这个类:

  class Tw(db.Model):
tags = db.ListProperty()
ip = db.StringProperty()

在我的主要请求处理程序中,我有以下内容:

 对于lst_of_cities中的城市:#这是20个对象的列表
dict_info = hw12.twitter(city)#这是获得每个字典10个字典列表的函数对象在列表中
datastore = Tw()#这是为db.model定义的类
datastore.tags.append(dict_info)#
datastore.ip = self.request.remote_addr
datastore.put()

data = Data.gql()#data实体,我们需要提取

我不确定这段代码是否可以写入。如果有人可以请大家帮忙,我们将非常感激。

解决方案

ndbPython数据库模型,它特别包含JsonProperty,它可以直接实现你想要的东西。

现在,你需要运行Python 2.7版本的App Engine目前还没有完全准备好生产,但现在看起来相当稳定,GvR自己似乎正在编写很多代码,这些代码对于代码质量来说是好兆头,而且我打算将它用于今年的某个时候生产......


I have a list of about 20 objects and for each object I return a list of 10 dictionaries.
I am trying to store the list of 10 dictionaries for each object in the list on GAE; I do not think I am writing the code correctly to store this information to GAE.
Here is what I have: Before my main request handler I have this class:

class Tw(db.Model):
  tags = db.ListProperty()
  ip = db.StringProperty()

In my main request handler I have the following:

for city in lst_of_cities: # this is the list of 20 objects
  dict_info = hw12.twitter(city) # this is the function to get the list of 10 dictionaries for each object in the list
  datastore = Tw() # this is the class defined for db.model
  datastore.tags.append(dict_info) # 
  datastore.ip = self.request.remote_addr
datastore.put()

data = Data.gql("") #data entities we need to fetch

I am not sure if this code is write at all. If anyone could please help it would be much appreciated.

解决方案

Since this was written, the App Engine has pushed out their experimental "ndb" Python database model, which contains in particular the JsonProperty, something that pretty well directly implements what you want.

Now, you need to be running the Python 2.7 version of the App Engine, which is still not quite ready for production, but it all seems pretty stable these days, GvR himself seems to be writing a lot of the code which bodes well for the code quality, and I'm intending to use this in production sometime this year...

这篇关于在GAE中存储字典列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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