Store a list of dictionaries in GAE

前端 未结 3 651
抹茶落季
抹茶落季 2020-12-21 06:30

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

3条回答
  •  悲&欢浪女
    2020-12-21 06:38

    Welcome to Stack Overflow!

    I see a few issues:

    1. Dictionaries are not supported value types for App Engine properties.
    2. You're only storing the last entity; the rest are discarded.
    3. You're using a ListProperty, but instead of appending each element of dict_info, you're doing a single append of the entire list.

    Since you can't store a raw dictionary inside a property, you need to serialize it to some other format, like JSON or pickle. Here's a revised example using 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)
    

    When you fetch the entity later, you can retrieve your list of dictionaries with pickle.loads(entity.tags).

提交回复
热议问题