这是我在代码中要做的错误
TypeError: unhashable type: 'list'
这是我的python字典列表对象。在我的代码tea_list_data中
[
{'bpl_division_id': 1, 'over_kgs': 2, 'worker_id': 1, 'names': 6.0, 'date': '2013-05-08', 'pss': 30.0,
'bpl_estate_id': 1, 'scrap': 3, 'bpl_company_id': 1, 'output': 15, 'type': 'tea'},
{'bpl_division_id': 1, 'over_kgs': 2, 'worker_id': 2, 'names': 6.0, 'date': '2013-05-08', 'pss': 30.0,
'bpl_estate_id': 1, 'scrap': 3, 'bpl_company_id': 1, 'output': 15, 'type': 'tea'},
'bpl_estate_id': 1, 'scrap': 3, 'bpl_company_id': 1, 'output': 15, 'type': 'rubber'}
]
而且我需要将其更改为这种类型的字典对象。因为我需要在某些条件下在openerp中创建记录。
下面显示我所需的输出
{'bpl_division_id': 1, 'over_kgs': 2, 'worker_id': 1, 'names': 6.0, 'date': '2013-05-08', 'pss': 30.0,
'bpl_estate_id': 1, 'scrap': 3, 'bpl_company_id': 1, 'output': 15, 'type': 'tea'},
{'bpl_division_id': 1, 'over_kgs': 2, 'worker_id': 2, 'names': 6.0, 'date': '2013-05-08', 'pss': 30.0,
'bpl_estate_id': 1, 'scrap': 3, 'bpl_company_id': 1, 'output': 15, 'type': 'tea'},
{'bpl_division_id': 1, 'over_kgs': 2, 'worker_id': 3, 'names': 6.0, 'date': '2013-05-08', 'pss': 30.0,
'bpl_estate_id': 1, 'scrap': 3, 'bpl_company_id': 1, 'output': 15, 'type': 'rubber'},
{'bpl_division_id': 1, 'over_kgs': 2, 'worker_id': 4, 'names': 6.0, 'date': '2013-05-08', 'pss': 30.0,
'bpl_estate_id': 1, 'scrap': 3, 'bpl_company_id': 1, 'output': 15, 'type': 'rubber'}
请帮助我解决此问题。
这里显示了我用来创建上述第一个结果的python代码
def temp_posting(self, cr, uid, ids, context=None):
tea_v = {}
tea_list_data = []
if context is None:
context = {}
bpl_division_id = context['bpl_division_id']
work_update_ids = self.pool.get('bpl.work.update').search(cr, uid, [('bpl_division_id', '=', bpl_division_id)])
work_update_obj = self.pool.get('bpl.work.update').browse(cr, uid, work_update_ids)[0]
if work_update_obj:
work_update_obj.write({'state': 'negotiation'})
for record in work_update_obj.selected_tea_workers_update_line_ids:
tea_list_data.append({'bpl_company_id': record.work_id.bpl_company_id.id, 'bpl_estate_id': record.work_id.bpl_estate_id.id, 'bpl_division_id': record.work_id.bpl_division_id.id, 'worker_id': record.worker_id.id, 'date': record.work_id.offered_date, 'type':'tea', 'names': 6.00, 'output':record.tea_total_kgs, 'over_kgs':2, 'scrap':3, 'pss':30.00})
for record in work_update_obj.selected_rubber_workers_update_line_ids:
tea_list_data.append({'bpl_company_id': record.work_id.bpl_company_id.id, 'bpl_estate_id': record.work_id.bpl_estate_id.id, 'bpl_division_id': record.work_id.bpl_division_id.id, 'worker_id': record.worker_id.id, 'date': record.work_id.offered_date, 'type':'rubber', 'names': 6.00, 'output':record.rubber_total_kgs, 'over_kgs':2, 'scrap':3, 'pss':30.00})
for record in work_update_obj.selected_sundry_workers_update_line_ids:
tea_list_data.append({'bpl_company_id': record.work_id.bpl_company_id.id, 'bpl_estate_id': record.work_id.bpl_estate_id.id, 'bpl_division_id': record.work_id.bpl_division_id.id, 'worker_id': record.worker_id.id, 'date': record.work_id.offered_date, 'type':'sundry', 'names':record.sundry_hrs_worked, })
for record in work_update_obj.selected_other_workers_update_line_ids:
tea_list_data.append({'bpl_company_id': record.work_id.bpl_company_id.id, 'bpl_estate_id': record.work_id.bpl_estate_id.id, 'bpl_division_id': record.work_id.bpl_division_id.id, 'worker_id': record.worker_id.id, 'date': record.work_id.offered_date, })
tea_v = {tea_list_data}
daily_transaction_master = self.pool.get('bpl.daily.transaction.master')
daily_transaction_master.create(cr, uid, tea_v, context=context)
return True
编辑*@Arya*请在这里关注
for record in work_update_obj.selected_tea_workers_update_line_ids:
tea_list_data.append({'bpl_company_id': record.work_id.bpl_company_id.id, 'bpl_estate_id': record.work_id.bpl_estate_id.id, 'bpl_division_id': record.work_id.bpl_division_id.id, 'worker_id': record.worker_id.id, 'date': record.work_id.offered_date, 'type':'tea', 'names': 6.00, 'output':record.tea_total_kgs, 'over_kgs':2, 'scrap':3, 'pss':30.00})
tea_v = {tea_list_data}
解决方案
您可以迭代列表以从列表中获取字典。
尝试这个:
for rec in your_list_of_dictionary:
print rec #this rec variable contains dictionary
您可以在每个for循环中创建记录,而不是在列表中追加记录。
像这样:
daily_transaction_master = self.pool.get('bpl.daily.transaction.master')
for record in work_update_obj.selected_tea_workers_update_line_ids:
vals = {'bpl_company_id': record.work_id.bpl_company_id.id, 'bpl_estate_id': record.work_id.bpl_estate_id.id, 'bpl_division_id': record.work_id.bpl_division_id.id, 'worker_id': record.worker_id.id, 'date': record.work_id.offered_date, 'type':'tea', 'names': 6.00, 'output':record.tea_total_kgs, 'over_kgs':2, 'scrap':3, 'pss':30.00}
daily_transaction_master.create(cr, uid, vals, context=context)`
您可以在每个循环中执行相同的操作。