Help

0

ver.14: Many2one field how to filter from Many2many

Avatar
SmithJohn45

in "Products" (model product_template) I have inherited and added a custom field "brand_id" of type "Many2many"

in my custom "OrderItems" I had a field "brand_id" as "Many2one", 

here user selects a "product" and for "brand" I want to 'filter' and show only "brands" which are associated to the selected "product" in "Products

please help how to filter it.

regards


Avatar
Discard
2 Answers
0
Avatar
Kiran Mohan
Best Answer

Hi ,

optional_product_ids is Many2many in Product Master

@api.onchange('product_id')
def onchange_product_id(self):
if self.product_id:
self.optional_id = False
optional_products = self.product_id.optional_product_ids.ids
if optional_products:
domain = [('id','in',optional_products)]
return {'domain':{'optional_id':domain}}
return {'domain':{'optional_id':[('id', 'in', [])]}}



6 Comments
Avatar
Discard
Avatar
SmithJohn45
-

thanks Kiran Mohan, will implement, check and give feedback here... :)

Avatar
SmithJohn45
-

hi @Kiran Moha, sorry for late in reply, was busy in other tasks, today i tried to implement the code as your's but it is showing error. i want to copy my all 3 model's .py code and your code implemented with some changes. the error is " ValueError: Invalid domain term ('id', 'in', 'tests.brands(1, 2, 3)') " (without double quotes). don't know where i am making mistake due to lack of knowledge, please help. note: I can post the code as mentioned but this will not as readable here without formatting. regards

Avatar
Kiran Mohan
-

optional_products = self.product_id.optional_product_ids.ids

Avatar
SmithJohn45
-

hi Kiran Mohan, this is same line as your first Answer here, if you want to see complete code i can post here as Answer ... because in Comment it is not possible imo.

Avatar
Kiran Mohan
-

Answer updated , it was optional_products = self.product_id.optional_product_ids earlier

Avatar
SmithJohn45
-

thanks Kiran Mohan and appreciate your passions to help me :) regards

0
Avatar
Karan BK
Best Answer

While defining brand_id  field in the OrderItems, you can give domain such as

In OrderItems model

brand_id = fields.Many2one(...., domain=[('id', 'in', product_id.brand_id.ids)])

where product_id is the product field.



Karan BK

Junior Odoo Developer

Tel: +353 1 886 5684 (IE) +44 121 285 5684 (UK)  +91 964 381 7554 (IN)

Target Integration | CRM ERP Cloud

Website | LinkedIn | Twitter | Facebook | YouTube | Instagram

Avatar
Discard