Ajuda

0

How to get ID from Many2Many from base object

Avatar
Jack Dane

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, 

1 Comentário
Avatar
Descartar
Avatar
Sehrish
-

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)

1 Resposta
0
Avatar
Hilar Andikkadavath
Melhor Resposta

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

2 Comentários
Avatar
Descartar
Avatar
Jack Dane
-

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,

Avatar
Jack Dane
-

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,