This question has been flagged
3 Replies
25556 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?)

Avatar
Discard
Best Answer

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)

(5)
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.

Avatar
Discard
Best Answer

Hi, 

You can watch following video for this:

https://youtu.be/ezH3ql5Dmx4

Thanks

Avatar
Discard
Author Best Answer

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.

Avatar
Discard

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}]]