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.

5

How to add records to one2many field using onchange event

By
Aldennis
on 5/23/13, 6:22 AM 12,310 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

8

Sandeep

--Sandeep--
6083
| 6 5 8
Delhi, India
--Sandeep--
Python OpenERP Developer http://in.linkedin.com/pub/sandeep-rana/3b/a16/786/
Sandeep
On 5/23/13, 8:25 AM

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
2

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/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

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: 5/23/13, 6:22 AM
Seen: 12310 times
Last updated: 9/8/16, 5:19 AM