This question has been flagged
3 Replies
12928 Views

Hello Odoo community! 

i want filter domain of product_id (many2one field) by some condition ,to do that i write compute field and in xml code , iuse compute field as domain , but it did not work . can anyone help me???

in  .py

def _menu(self):

day = self.env['lunch.menu'].search([]).mapped('day')
order_day = self.env['lunch.order'].search([]).mapped('date')
day_name = datetime.datetime.now().strftime('%A')
for rec in self:
    for d in range (0,len(day)):
        if  day[d] == day_name:
            menu = self.env['lunch.menu'].search([('day' ,'=' ,day_name)]).mapped('product.id')
            rec.product_c = menu 


product_c = fields.Char('CProduct', compute = '_menu')
product_id = fields.Many2one('lunch.product', 'Product', required=True)


in  XML:

<field name='product_c' >

<field name='product_id' domain='product_c'>


Avatar
Discard
Best Answer

Hi

Try this...



def _menu(self):

res = {}
res['domain'] = {'field_name': [('id', '=', value)]}
return res

day = self.env['lunch.menu'].search([]).mapped('day')
order_day = self.env['lunch.order'].search([]).mapped('date')
day_name = datetime.datetime.now().strftime('%A')
for rec in self:
    for d in range (0,len(day)):
        if  day[d] == day_name:
            menu = self.env['lunch.menu'].search([('day' ,'=' ,day_name)]).mapped('product.id')
            rec.product_c = menu 


product_c = fields.Char('CProduct', compute = '_menu')
product_id = fields.Many2one('lunch.product', 'Product', required=True)

Avatar
Discard
Best Answer

if you want to change your domain base on product_id(many2one) field then please use on change method for it base on Product_id(many2one)

then you can easily return domain like 

res = {}
res['domain'] = {'field_name': [('id', '=', value)]}
return res
Avatar
Discard
Best Answer

Hi,

This will be helpful for you - https://www.linkedin.com/pulse/how-set-dynamic-domain-filter-many2one-field-odoo-rishan-malaka/

Avatar
Discard
Author

hi @Rishan Malaka

thanks for your reply .i write on_change function as you said but it did not work can you help me?

@api.onchange('date')

def _onchange_menu(self):

res={}

self.write({'test1':'ok'})

day = self.env['lunch.menu'].search([]).mapped('day')

order_day = self.env['lunch.order'].search([]).mapped('date')

day_name = datetime.datetime.now().strftime('%A')

for d in range (0,len(day)):

if day[d] == day_name:

menu = self.env['lunch.menu'].search([('day' ,'=' ,day_name)]).mapped('product.id')

res['doamin']={'product_c':[('id','in',menu.ids)]}

return res