Odoo Help


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.


OnChange event of o2m and m2m

Antonio Buric
on 3/2/13, 2:10 PM 7,487 views

Hi all,

is there any way to find out what record is being added/removed in an onchange event set on a one2many or many2many field?

The problem I ran into is the following - I have a many2many field and a one2many field on the form. Adding a record to m2m field should add one or more records to the o2m field. Using the tuple notation in an onchange event for inserting/deleting records from o2m is insufficient here, I covered most of the cases in the onchange method, but I can't get it right when in edit of an existing master record with saved m2m and o2m entries (I have to know which ID is being added or removed in the onchange event, is anything like that remotely possible - some workaround perhaps?)


Vasiliy Birukov

--Vasiliy Birukov--
| 6 6 8
Russian Federation
--Vasiliy Birukov--

Help develop and implement any project on base Odoo.

Vasiliy Birukov
On 3/4/13, 1:34 AM

Send x2many as parametr to onchange metod. It will have list of records as following: [[Mode, Option, {'field_name':field_value_record1, ...}], [Mode, Option, {'field_name':field_value_record2, ...}]]

Combination of Mode, Options and Fields Values define state of x2many records.

(0, 0, { values }) link to a new record that needs to be created with the given values dictionary

(1, ID, { values }) update the linked record with id = ID (write values on it)

(2, ID) remove and delete the linked record with id = ID (calls unlink on ID, that will delete the object completely, and the link to it as well)

(3, ID)
cut the link to the linked record with id = ID (delete the relationship between the two objects but does not delete the target object itself)

(4, ID) link to existing record with id = ID (adds a relationship)

unlink all (like using (3,ID) for all linked records)

(6, 0, [IDs])
replace the list of linked IDs (like using (5) then (4,ID) for each ID in the list of IDs)

For example: [4,5,False] - define existing records in DB with id=5

[0,0,{'name':'new record'} - difine new record with field "name"="new record", that will be created in DB, when holder of x2many will be saved to DB.

Antonio Buric
On 3/4/13, 5:57 PM

thanks for the response. however, I must have not been clear enough with my question.

The problem is that I always get something like [4,5,False] as an input to onchange method and it's not possible to find out which ID is being inserted or deleted. for example, we have 3 records in the many2many field with IDs 7,8,9. If we remove record with ID = 9, in onchange method we will get this [4,[7,8],False]. If instead we add a record with ID = 10, we would get [4,[7,8,9,10],False]. From that, there is no way to determine which ID the user added or deleted in an event that trigger the onchange method.

If you have only exist records it will be: [[4,7,False],[4,8,False],[4,9,False]]. If you remove in form ID=9 it will be: [[4,7,False],[4,8,False],[2,9,False]]. If you add in form new record it will be: [[4,7,False],[4,8,False],[2,9,False],[0,0,{values}]]

Vasiliy Birukov
on 3/5/13, 12:26 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

0 follower(s)


Asked: 3/2/13, 2:10 PM
Seen: 7487 times
Last updated: 3/27/15, 5:36 PM