Skip to Content
Menu
This question has been flagged
1 Reply
5748 Views

Hello,


I have put an onchange method on a Many2many field. However whenever I add / change the values ​​I want to search based on the id of the object not the id of the Many2many object, is this possible?


As the Id is always returning "newId", which I assume is the id of the Many2many relation table and not the base table.


Here is the main line of code:
[student.id for student in self.student_ids]


Thanks, 

Avatar
Discard

example_ids = fields.Many2many('model.a', string="Many2many String")

# onchange method

real_object_list = []

for data in self.example_ids:

real_object_list.append(data.real_object_id_from_many2_many_table)

Best Answer

If the record is not saved, then you will get the NewID and for a saved record, you can easily iterate with selected objects in lines. That's because, oncahnge method works on the fly and it is on client-side until the record is saved.

The same scenario happens if we are trying to domain out the selected records in O2x or M2x relations. If its in db, then we will get the ids, else possibly NULL or tempID values.

You can easily figure out the object ids by using ODOO inbuilt methods.

eg: self['many2many_field'].mapped('object_id').ids

Avatar
Discard
Author

Hello Hilar,

Thank you for the response. I have tried your answer but I am getting a keyerror on "object_id". For both:

self["student_ids"].mapped("object_id").ids

&

self.student_ids.mapped("object_id").ids

Thanks,

Author

Hello Hilar,

This is strange, I have saved a record which has a set of students in the group (commented out the onchange method).

Then I added the onchange method back into the model and it still says that they are "newId" even though the record has been saved with them in. In the tree view, I can even see the ID as the technical name "id". So id should be returning the id of the student as I am iterating through student ids, but it returns "newId".

Thanks,

Related Posts Replies Views Activity
1
Aug 21
1319
1
Nov 24
150
1
Feb 24
839
1
Apr 23
1470
0
Jul 24
1503