Bỏ qua để đến Nội dung
Menu
Câu hỏi này đã bị gắn cờ
2 Trả lời
11220 Lượt xem

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


Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

Hi,

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


Thanks

Ảnh đại diện
Huỷ bỏ
Tác giả

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

Câu trả lời hay nhất

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)​

Ảnh đại diện
Huỷ bỏ
Bài viết liên quan Trả lời Lượt xem Hoạt động
1
thg 6 19
7048
1
thg 8 24
7769
1
thg 2 25
20852
0
thg 7 25
827
1
thg 5 25
1554