Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

0

How to add dictionary (list object) to dictionary object in Python [Closed]

By
priyankahdp
on 5/8/13, 7:22 AM 5,482 views

The Question has been closed

by
Sudhir Arya (SA)
on 03/06/2014 09:07:14

here is my error when i going to do that in my code

TypeError: unhashable type: 'list'

Here is my python dictionary list object.

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

And i need to change it to this type dictionary object.because i need to create records in openerp with some conditions.

below show my required output

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

please help me to sort this issue.

here shows my python code that i used to create above 1st result

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
3

Sudhir Arya (SA)

--Sudhir Arya (SA)--
10150
| 6 8 8
Ahmedabad, India
--Sudhir Arya (SA)--

Working as an OpenERP/Odoo developer and a Team Leader 

Top 5 Odoo contributor On Stackoverflow

LinkedIn

Blog

Stackoverflow

Sudhir Arya (SA)
On 5/8/13, 7:28 AM

You can iterate list to get dictionary from list.

Try this:

for rec in your_list_of_dictionary:
    print rec #this rec variable contains dictionary

thanks Arya..

priyankahdp
on 5/8/13, 7:49 AM
3

Naresh Soni (nch)

--Naresh Soni (nch)--
Tiny ERP Pvt Ltd
http://www.odoo.com
1281
| 2 2 4
India
--Naresh Soni (nch)--

Team Lead, Odoo(Formerly OpenERP) Support & OPW

Naresh Soni (nch)
On 5/8/13, 7:46 AM

Remove this tea_v = {tea_list_data}

and replace this line daily_transaction_master.create(cr, uid, tea_v, context=context)

By:

For record in tea_list_data:
   daily_transaction_master.create(cr, uid, record, context=context)

Thanks,

thanks dear Soni..its sorted

priyankahdp
on 5/8/13, 9:03 AM

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

0 follower(s)

Stats

Asked: 5/8/13, 7:22 AM
Seen: 5482 times
Last updated: 3/16/15, 8:10 AM