2013-05-08 92 views
0

這裏是我的錯誤,當我打算這樣做在我的代碼如何添加字典(列表對象)到字典對象在Python

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'} 
] 

,我需要將其更改爲這種類型的詞典object.because我需要創建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'} 

請幫我解決這問題。

這裏表明我用來創建上述第一個結果

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 

EDITED * @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} 
+1

該錯誤表明,您以某種方式將列表分配爲字典的關鍵字,這在python中是不允許的。 – 2013-05-08 09:32:14

+1

作爲輸出需要的是,逗號分隔的字典值,它不是python中的有效數據結構。 – 2013-05-08 09:38:50

+0

尊敬的Ankit ,, 請指正我糾正它 – 2013-05-08 09:50:44

回答

2

你可以在我的Python代碼迭代列表以從列表中獲取字典。

試試這個:

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)` 

你可以做同樣的事情在你的每一個循環。

+0

感謝arya.its意味着需要多次使用create方法? – 2013-05-08 10:03:51

+1

是的。那就是我的意思。 – 2013-05-08 10:05:39

+0

感謝其工作良好的艾莉亞。 關於http://stackoverflow.com/questions/16434990/openerp-7-reports-error的任何想法? – 2013-05-08 10:37:12

0

這裏是最終編輯的代碼,現在它的工作完美。

def temp_posting(self, cr, uid, ids, context=None): 
    tea_v = {} 
    rubber_v = {} 
    sundry_v = {} 
    other_v = {} 

    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'})   

    daily_transaction_master = self.pool.get('bpl.daily.transaction.master') 

    for record in work_update_obj.selected_tea_workers_update_line_ids: 
     tea_v = {'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, tea_v, context=context) 
    for record in work_update_obj.selected_rubber_workers_update_line_ids: 
     rubber_v = {'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}  
     daily_transaction_master.create(cr, uid, rubber_v, context=context) 
    for record in work_update_obj.selected_sundry_workers_update_line_ids: 
     sundry_v = {'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, }  
     daily_transaction_master.create(cr, uid, sundry_v, context=context) 
    for record in work_update_obj.selected_other_workers_update_line_ids: 
     other_v = {'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, } 
     daily_transaction_master.create(cr, uid, other_v, context=context) 
    return True