Keep a list of dictionaries in GAE

I have a list of about 20 objects, and for each object I return a list of 10 dictionaries.
I am trying to save a list of 10 dictionaries for each object in the GAE list; I do not think that I am writing code correctly for storing this information in GAE.
Here is what I have: In front of 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'm not sure if this code writes at all. If anyone could help, that would be very grateful.

+3
source share
3 answers

Welcome to stack overflow!

I see several problems:

  • App Engine.
  • ; .
  • ListProperty, dict_info .

, , , JSON 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).

+4

, 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)

PickleProperty commons.py, :

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
+4

, App Engine Python "ndb", , , JsonProperty, -, , .

Python 2.7 App Engine, , , GvR, , , , ...

+4

All Articles