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

(Please help me - i got only partial output) How to update a one2many field in the onchange many2one field?( i want to create a new row in each onchange.)

By
rosey
on 1/5/15, 6:51 AM 1,216 views

field 1 : many2one field

field 2 is a char field inside a one2many field

When i select field 1 value, field 2 must be updated. How can i update it using onchange or any other option?
 

My code:

class hr_employee(osv.osv):

      _inherit = 'hr.employee'

'own_location' : fields.many2one('location.location','Own location'),

'location_line_ids': fields.one2many('employee.location', 'location_line_id', 'Location'),

When i select 'own_location' , a onchange function will call and i want to update the 'current_location' field in the below table. How can i do that?

class employee_location(osv.osv):
      _name='employee.location' 
      _columns={              
                'current_location':fields.char('Current location'),
                'new_location':fields.many2one('location.location','New location'),
                'start_date' : fields.date('Start Date'),
                'end_date' : fields.date('End Date'),    
                'location_line_id':fields.many2one('hr.employee','Employee location',ondelete='cascade'),  
                }  

I tried many ways but no output. Please any one help me to solve this.

Below atchuthan code working perfectly.

I need add a new row on each onchange & update the value of current_location in a new row on each onchange.

How can i update the value of current_location in a new row on each onchange?

0

Atchuthan - Technical Consultant, Sodexis Inc

--Atchuthan - Technical Consultant, Sodexis Inc--
3883
| 5 3 8
Puducherry, India
--Atchuthan - Technical Consultant, Sodexis Inc--
Passionate coder in Python/Odoo(formerly known as OpenERP).

Contact me via GMail: atchuthantu@gmail.com via Skype: atchuthan_skype
via Linkedin: http://in.linkedin.com/in/atchuthantu
Atchuthan - Technical Consultant, Sodexis Inc
On 1/5/15, 7:19 AM

Try to update the field as if you are trying to update a list of records i.e. List manipulation.

    def onchange_own_location(self, cr, uid, ids, own_location, location_line_ids, context=None):
        res = {}
        for line in location_line_ids:
            if not line[0] == 2:
                if line[1]:
                    line[0] = 1
                    if not isinstance(line[2], dict):
                        line[2] = {}
                    line[2]['field2']= field1
                else:
                    line[2]['field2'] = field1
            if sale_ok:
                line[2]['show_price'] = sale_ok
        res['location_line_ids'] = location_line_ids
        return {'value': res}

NOTE: more info on one2many and many2many manipulation is available at Odoo documentation (https://www.odoo.com/documentation/8.0/reference/orm.html#model-reference)

One2many and Many2many use a special “commands” format to manipulate the set of records stored in/associated with the field.

This format is a list of triplets executed sequentially, where each triplet is a command to execute on the set of records. Not all commands apply in all situations. Possible commands are:

(0, _, values)

adds a new record created from the provided value dict.

(1, id, values)

updates an existing record of id id with the values in values. Can not be used increate().

(2, id, _)

removes the record of id id from the set, then deletes it (from the database). Can not be used in create().

(3, id, _)

removes the record of id id from the set, but does not delete it. Can not be used onOne2many. Can not be used in create().

(4, id, _)

adds an existing record of id id to the set. Can not be used on One2many.

(5, _, _)

removes all records from the set, equivalent to using the command 3 on every record explicitly. Can not be used on One2many. Can not be used in create().

(6, _, ids)

replaces all existing records in the set by the ids list, equivalent to using the command 5followed by a command 4 for each id in ids. Can not be used on One2many.

 

hai. thanks for the answer. Now i getting the location id. how can i get the location name?

rosey
on 1/5/15, 7:35 AM

you can get the value from "own_location" field by own_location.name

Atchuthan - Technical Consultant, Sodexis Inc
on 1/5/15, 7:39 AM

Can you please expalin the code?

rosey
on 1/5/15, 7:42 AM

Thanks for your reply. Your code is working fine. On each onchange i want to create a new row in my one2many field. How is it possible?

rosey
on 1/5/15, 11:43 PM

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

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

Question tools

1 follower(s)

Stats

Asked: 1/5/15, 6:51 AM
Seen: 1216 times
Last updated: 8/25/15, 11:37 AM