我有一個python字典,我試圖通過pickle屬性在GAE Gql Datastore中存儲。用PickleProperty在GAE中存儲字典
我試圖按照這一問一答, Store a list of dictionaries in GAE
,但它不工作對我下面的代碼..我拿到那有我的名單
我安裝鹹菜屬性和問題的錯誤數據模型類,如下所示,
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
感謝您的幫助提前
您的make_value函數中的「m」是否爲錯字?我不認爲你需要pickle.mloads()函數(來自「marshal」模塊),而是常規loads()函數。 – Blckknght
在您的代碼中與'MDB'一樣是'MasjidDB'嗎? – aschmid00
是的。和@Blckknght我有我的原因是我正在嘗試不同的功能,但謝謝你的幫助,我得到它的工作! – user772401