我有一个 python 字典,我试图通过 pickle 属性存储在 GAE Gql Datastore 中。
我试图按照这个问题和答案, 在 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
提前感谢您的帮助