GAE db.Model无法存储元组列表。因此,当我将列表拉回实体时,我需要将列表保存为json.dumps字符串并将json.loads字符串保存。我想自动化这个。这是我的尝试:
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)
当我尝试设置实体的A,B,C属性时:
BadValueError: Property C must be a str or unicode instance, not a list
答案 0 :(得分:3)
如果您不需要存储非常大的JSON,我建议您查看NDB。它内置了JsonProperty:https://developers.google.com/appengine/docs/python/ndb/properties#types。
我还在App Engine Googl Group中发现了一篇有趣的文章,讨论了一种更有效的存储大型JSON的方法:https://groups.google.com/forum/#!msg/google-appengine/WPfAvHDGNjQ/XfakEMm1qzoJ