Ir al contenido
Menú
Se marcó esta pregunta
2 Respuestas
11142 Vistas

Dear all,

I am working on Odoo v12 and have a problem with a many2many field.
My many2many field is related to "res.users" and since it is a many2many, for some records I have more than one value that are passed to the actual object.
I have a variable and need to check if the variable value is on the many2many field,  and remove all other records from the object.

    my_variable = 42
    my_relatedfield_ids.ids = res.users(9, 42)

From the above result for the "my_relatedfield_ids.ids", how can I remove the value "9" and keep the value "42" which is the desired value to work with?

For obtaining the "my_relatedfield_ids" I have used:

     filtered_result = object.mapped("my_relatedfield_ids")     #Where "object" contains the results from a search with a specific domain using self.env...

After this, if I print "filtered_result" I get:

     print(filtered_result)      : res.users(9, 42)
    print(filtered_result.ids) : res.users(9, 42)

My related field is declared as:

my_relatedfield_ids = fields.Many2many('res.users', relation='relatedmodel_rel_users', column1='id',column2='name')

What I need to do is to remove the excess values from this object and leave only the value "42" which is the value I need to work with and it is on "my_variable".

I need to keep it like: res.users(42)

Can you help me please?

Best regards

Paulo


Avatar
Descartar
Mejor respuesta

Hi,

For removing value from many2many field please refer the answer given for this question: Removing elements from many2many field


Thanks

Avatar
Descartar
Autor

Thank you @Niyas,

I have solved my problem and reading the url you sent I have found additional solutions for my problem.

Thank you once again

Best regards

Paulo

Mejor respuesta

You can write as following to get only my_variable from my_relatedfield_ids.


only_my_variable_record = my_relatedfield_ids.filtered(lambda l : l.id == my_variable)​

Avatar
Descartar
Publicaciones relacionadas Respuestas Vistas Actividad
1
jun 19
6969
1
ago 24
7697
1
feb 25
20760
0
jul 25
648
1
may 25
1424