GAE db.Model cannot store a list of tuples. Thus I need to save the list as a json.dumps string and json.loads the string when I pull the list back out of the entity. I would like to automate this. Here is my attempt:
class example(db.Model):
someAttr = db.StringListProperty()
A = db.StringProperty()
B = db.StringProperty()
C = db.StringProperty()
def __setattr__(self, name, value):
#convert json to string for storage
if name in ("A", "B", "C"):
value = json.dumps(value)
#call default set method
return super(Quote, self).__setattr__(name, value)
def __getattribute__(self, name):
#convert string to json for retrieval
if name in ("A", "B", "C"):
val = super(Quote, self).__getattribute__(name)
return json.loads(val)
else:
# default get behavior
return super(Quote, self).__getattribute__(name)
When I try to set the A,B,C attributes of the entity I get:
BadValueError: Property C must be a str or unicode instance, not a list
If you don’t need to store very large JSONs, I’d suggest you check out NDB. It has built-in JsonProperty: https://developers.google.com/appengine/docs/python/ndb/properties#types.
I also came across an interesting post in App Engine Googl Group that talks about a more efficient way of storing large JSONs: https://groups.google.com/forum/#!msg/google-appengine/WPfAvHDGNjQ/XfakEMm1qzoJ