This question has been flagged
1 Reply
4735 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?

Avatar
Discard
Best Answer

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.

 

Avatar
Discard
Author

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

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

Author

Can you please expalin the code?

Author

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?