【问题标题】:Store a list of dictionaries in GAE在 GAE 中存储字典列表
【发布时间】:2011-05-03 18:30:41
【问题描述】:

我有一个大约 20 个对象的列表,对于每个对象,我返回一个包含 10 个字典的列表。
我正在尝试为 GAE 列表中的每个对象存储 10 个字典的列表;我认为我没有正确编写代码来将此信息存储到 GAE。
这是我所拥有的: 在我的主要请求处理程序之前,我有这个类:

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

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

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

我不确定这段代码是否是写的。如果有人可以请帮助,将不胜感激。

【问题讨论】:

  • 听起来您的列表相当静态。为什么不直接将其存储在代码或数据文件中?

标签: python google-app-engine google-cloud-datastore


【解决方案1】:

欢迎来到 Stack Overflow!

我发现了一些问题:

  1. 对于 App Engine 属性,字典不是 supported value types
  2. 您只存储最后一个实体;其余的被丢弃。
  3. 您使用的是 ListProperty,但不是追加 dict_info 的每个元素,而是对整个列表进行一次追加。

由于您无法在属性中存储原始字典,因此您需要将其序列化为其他格式,例如 JSON 或 pickle。这是一个使用pickle的修改示例:

from google.appengine.ext import db
import pickle

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

entities = []
for city in lst_of_cities:
  dict_info = hw12.twitter(city)
  entity = Tw()
  entity.tags = db.Blob(pickle.dumps(dict_info))
  entity.ip = self.request.remote_addr
  entities.append(entity)

db.put(entities)

当您稍后获取实体时,您可以使用pickle.loads(entity.tags) 检索您的字典列表。

【讨论】:

    【解决方案2】:

    当我处理 Google App Engine 不直接支持的数据类型(如字典或自定义数据类型)时,我通常采用方便的PickleProperty

    from google.appengine.ext import db
    import pickle
    
    class PickleProperty(db.Property):
        def get_value_for_datastore(self, model_instance):
            value = getattr(model_instance, self.name, None)
            return pickle.dumps(value)
    
        def make_value_from_datastore(self, value):
            return pickle.loads(value)
    

    一旦在 commons.py 模块中声明了 PickleProperty 类,您就可以使用它来存储您的自定义数据,如下所示:

    from google.appengine.ext import db
    from commons import PickleProperty
    
    class Tw(db.Model):
      tags = PickleProperty()
      ip = db.StringProperty()
    
    entities = []
    for city in lst_of_cities:
      dict_info = hw12.twitter(city)
      entity = Tw()
      entity.tags = dict_info
      entity.ip = self.request.remote_addr
      entities.append(entity)
    
    db.put(entities)
    

    要检索数据,请使用:

    entity.tags
    

    【讨论】:

      【解决方案3】:

      自编写以来,App Engine 推出了他们的实验性“ndb”Python 数据库模型,其中特别包含 JsonProperty,它可以很好地直接实现您想要的东西。

      现在,您需要运行 App Engine 的 Python 2.7 版本,它还没有完全准备好投入生产,但现在一切似乎都很稳定,GvR 自己似乎正在编写很多预示着的代码代码质量很好,我打算今年某个时候在生产中使用它......

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-12-20
        • 1970-01-01
        • 1970-01-01
        • 2015-05-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多