跳至内容
菜单
此问题已终结
3 回复
14976 查看

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'>


形象
丢弃
最佳答案

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)

形象
丢弃
最佳答案

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
形象
丢弃
最佳答案

Hi,

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

形象
丢弃
编写者

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

相关帖文 回复 查看 活动
0
8月 19
3381
1
11月 18
5692
5
2月 18
4809
2
7月 24
2436
1
6月 24
4951