Odoo Help

5

How to add records to one2many field using onchange event

By
Aldennis
on 5/23/13, 6:22 AM 16,397 views

In stock.picking form, I would like to know how can I add an item to the one2many field move_lines using an onchange event on the partner_id field.

Both of the field with the onchange event and the one2many field that needs to add records belongs to the same class

9

Hi Aldennis

suppose your one2manyfield name is my_field then

define list in first ex .. r=[]

then

r.append({'first_field_ino2m':'my vlaue','second......':'my second value'})

you can add more field availble in That one2manyobject for another record just continuous append same after all append completed just add rinmy_field':r,

exm. vals.update({'my_field':r})

Thanks
Sandeep

This solution allows you to use the on_change even if you just clicked create and haven't saved your record yet. +1 and setted as accepted answerd. Thanks Sandeep

Camptocamp SA
on 8/21/13, 11:43 AM

Thanks @ Sandeep +1

Ankit H Gandhi(AHG)
on 8/3/16, 6:13 AM
4
Sudhir Arya (SA)
On 5/23/13, 10:16 AM

You need to return all IDs in one list with your one2many field in values.

Syntax:

return {'value': {'your_one2many_field': list_of_ids}}

For example my_emp_ids is my one2many field of hr.employee and I want to fill it when I will select Department. I want all employees of selected Depratment.

def onchange_department(self, cr, uid, deprt_id):
    employee_ids = self.search(cr, uid, [('department_id', '=', deprt_id)])
    return {'value': {'my_emp_ids': employee_ids}}

I've tried your code , it's working . but the employee filled is not based on their department , but based on the id of class that contain deprt_id . Example : in my departments , Admin have ID 1 , Programmer have ID 2 , Engineer have ID 3 . When I choose Programmer Department , the printed employees on my_emp_ids is not programmer dept employee , but Admin dept employee . When I checked on PostgreSQL Database , the deprt_id shows correct id ( same id with department_id ) . I assume that the printed employee based on id of deprt_id and not the deprt_id . Am I making mistake ? please help

Nanda Kanoko
on 7/15/13, 2:13 AM

This code is full work Thank you @ Sudhir sir +1 and up voted.

Ankit H Gandhi(AHG)
on 11/23/15, 7:15 AM

About This Community

This platform 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.

Register

Odoo Training Center

Access to our E-learning platform and experience all Odoo Apps through learning videos, exercises and Quizz.

Test it now

Question tools

2 follower(s)

Stats

Asked: 5/23/13, 6:22 AM
Seen: 16397 times
Last updated: 8/22/17, 2:14 AM