Skip to Content
Menu
This question has been flagged

hello guys, i want to ask you a question, like example 


i have model a with one2many field to model ab and ab have many2one field to model c then in c i have field selection lets say c_type with these options:

1. ca, CA

2. cb, CB


and i put selection field in model a with the same option like in c_type but im added 1 option more so it will be like this:

1. ca, CA

2. cb, CB

3. all, CA & CB


the question is how can i filter domain on field c in model ab if the selection c_type in model a is "all" (that means i want to get record c with c_type in ['ca', 'cb'])? thanks

Avatar
Discard
Best Answer

 Hello, let's say you are trying to filter products based on the price list.

You can create a new field:

products_pricelist = fields.One2many('product.product', 'id', 'Pricelist Products', compute='_calc_price_list_products')

and then use a compute function to bring all the products from that price list:

# To filter the domain of products based on price list
@api.depends('pricelist_id')
def _calc_price_list_products(self):
product_ids = []
price_list_line = self.env['product.pricelist.item'].search([('pricelist_id', '=', self.pricelist_id.id)])
if price_list_line:
price_list_lines_product_id = self.env['product.pricelist.item'].search([('pricelist_id', '=', self.pricelist_id.id)]).mapped("product_tmpl_id").ids
if price_list_lines_product_id:
product_ids = self.env['product.product'].search([('product_tmpl_id', 'in', price_list_lines_product_id)]).ids
else:
self.products_pricelist = False
return
for
price_line in price_list_line:
if price_line.base == 'pricelist':
if price_line.applied_on == '3_global':
base_lines_product_id = self.env['product.pricelist.item'].search([('pricelist_id', '=', price_line.base_pricelist_id.id)]).mapped("product_tmpl_id").ids
product_line_ids = self.env['product.product'].search(['&',('product_tmpl_id', 'in', base_lines_product_id),('id','not in', product_ids)]).ids
product_ids = product_ids + product_line_ids
if price_line.applied_on == '2_product_category':
base_lines_product_id = self.env['product.pricelist.item'].search([('pricelist_id', '=', price_line.base_pricelist_id.id)]).mapped("product_tmpl_id").ids
product_line_ids = self.env['product.product'].search(['&', '&', ('product_tmpl_id', 'in', base_lines_product_id), ('categ_id', '=', price_line.categ_id.id), ('id','not in', product_ids)]).ids
product_ids = product_ids + product_line_ids
if product_ids:
self.products_pricelist = product_ids

So after that you can filter in the other field:
<xpath expr="//tree/field[@name='product_id']" position="attributes">
<attribute
name="domain">[('id', 'in', products_pricelist )]</attribute>
</xpath>
Hope it helps.

Regards.
Avatar
Discard
Related Posts Replies Views Activity
1
Oct 20
2350
2
Dec 21
4527
6
Aug 20
6101
1
Dec 19
11827
2
Dec 23
30868