Skip to Content
Menu
This question has been flagged
2 Replies
9764 Views

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
Discard
Best Answer

Hi,

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


Thanks

Avatar
Discard
Author

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

Best Answer

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
Discard
Related Posts Replies Views Activity
1
Jun 19
5967
1
Aug 24
5869
1
Jul 19
18831
1
Jun 24
3685
0
Feb 24
534