How to create many2many field in odoo?
your_many2many_field = fields.Many2many('model.name','your_many2many_table_name', 'column_1','columns_2', string="My many2many Field")
Code Description
"model.name" is the name of that model which you want to select/show as many2many.
"your_many2many_table_name" is the name of third table, you can write any name as you want.
"column_1" will be parent table reference or id.
"columns_2" will be many2many table reference or id.
Add domain on many2many field in odoo.
Now we will add domain on many2many field.
@api.multi
def _add_domain(self):
user_obj = self.env['res.users'].search([('department_id','=',self.env.user.department_id.id)])
if user_obj:
domain = [('id', 'in', user_obj.ids)]
else:
domain = [('id', '=', -1)]
return domain
your_many2many_field = fields.Many2many('model.name','your_many2many_table_name', 'column_1','columns_2', string="My many2many Field",domain=_add_domain)
Code Description
In above code we have added "domain=_add_domain" and create a function that will return our desired ids which we want to load/filter on many2many field.
Load default value on many2many field.
@api.multi
def _load_default_user(self):
user_obj = self.env['res.users'].search([('department_id','=',self.env.user.department_id.id)])
if user_obj:
return user_obj.ids
else:
return []
your_many2many_field = fields.Many2many('model.name','your_many2many_table_name', 'column_1','columns_2', string="My many2many Field",default=_load_default_user)