我有一个python字典,我试图通过pickle属性存储在GAE Gql数据存储区中。
我试着按照这个问题回答, Store a list of dictionaries in GAE
但它不适用于我的代码。我收到错误,我的列表出现问题
我设置了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.mloads(value)
class MDB(db.Model):
Name = db.StringProperty(required=True)
Times = PickleProperty()
created = db.DateTimeProperty(auto_now_add = True)
我的代码中有一个函数,它插入数据并将其读出
def m_time_manage(m=""):
if not m:
r = db.GqlQuery("select * from MDB")
else:
#find specific masjid requested
r = db.GqlQuery("select * from MDB where Name = %s" % (s))
#if masjid time data doesn't exist in db throw in placeholders...
if r is None:
times = {}
times['a'] = "8:49"
times['b'] = "10:19"
times_entry = MDB(Name="abc",Times=times)
times_entry.put()
r = db.GqlQuery("select * from MasjidDB")
return (r[0].Name, r[0].Times)
在我的get函数中,我按如下方式调用m_time_manage()ftn,
(mName, times) = m_time_manage()
self.write(times)
我得到这个错误,那就是pickle.loads和文件结束错误......完整的错误是但是我不确定如何在python中存储字典,
line 31, in make_value_from_datastore
return pickle.loads(value)
File "/usr/lib/python2.7/pickle.py", line 1382, in loads
return Unpickler(file).load()
File "/usr/lib/python2.7/pickle.py", line 858, in load
dispatch[key](self)
File "/usr/lib/python2.7/pickle.py", line 880, in load_eof
raise EOFError
感谢您提前提供任何帮助
答案 0 :(得分:6)
我建议切换到NDB并使用其内置的PickleProperty。见https://developers.google.com/appengine/docs/python/ndb/properties