1

我正在尝试将结构化数据腌制和解封到 ndb.PickleProperty() 属性中,如下所示:

month = MonthRecord.get_or_insert(month_yr_str, parent=ndb.Key('Type','Grocery'), record=pickle.dumps(defaultdict(list_list)))
names_dict = pickle.loads(month.record) # unpickle for updating
# ...                                   # some modifications on names_dict
month.record = pickle.dumps(names_dict) # pickle
month.put()                             # commit changes

其中模型 MonthRecord 定义为:

class MonthRecord(ndb.Model):
    record = ndb.PickleProperty() # {name: [[date&time],[expenses]]}

和 list_list 为:

def list_list(): # placeholder function needed by pickle at module level
    return [[],[]]

第一次运行正常(在 get_or_insert 中命中插入案例,创建一个新的 MonthRecord 实体)。但是,在后续运行期间(即要记录的当月内的新费用)出现以下错误:

Traceback (most recent call last):
  File "C:\GAE_Projects\qb_lite\fin.py", line 31, in update_db
    names_dict = pickle.loads(month.record)
  File "C:\Python27\lib\pickle.py", line 1382, in loads
    return Unpickler(file).load()
  File "C:\Python27\lib\pickle.py", line 858, in load
    dispatch[key](self)
  File "C:\Python27\lib\pickle.py", line 1133, in load_reduce
    value = func(*args)
TypeError: __init__() takes exactly 4 arguments (1 given)

关于错误原因的任何想法?

4

1 回答 1

2

您不必腌制您的对象,这将由 PickleProperty 处理。

代替:

month = MonthRecord.get_or_insert(
    month_yr_str,
    parent=ndb.Key('Type','Grocery'),
    record=pickle.dumps(defaultdict(list_list)))

做:

month = MonthRecord.get_or_insert(
    month_yr_str,
    parent=ndb.Key('Type','Grocery'),
    record=defaultdict(list_list))

酸洗和解酸将在内部进行。

于 2013-05-14T04:58:12.320 回答