How to Make many2many Field Record Checkbox Checked Automatically by a Condition??

How to Make many2many Field Record Checkbox Checked Automatically by a Condition??

Solution:

Step 1: Define default method for that Many2many field.

Step 2: In that default method return ids of records which has to be checked by default.

This will automatically check that record under Many2many field.

 

Example:

In Product tab under “Manufacturing” menu,you can see field Routes(Many2many) under Page “Inventory”.This field has widget=”many2many_checkboxes”.

Out of Make to order,Buy and Manufacture only “Buy” is selected by default.

 

Routing_id field –>

‘route_ids’: fields.many2many(‘stock.location.route’, ‘stock_route_product’, ‘product_id’, ‘route_id’, ‘Routes’, domain=”[(‘product_selectable’, ‘=’, True)]”,

help=”Depending on the modules installed, this will allow you to define the route of the product: whether it will be bought, manufactured, MTO/MTS,…”),

route_ids = fields.Many2many(default=lambda self: self._get_buy_route())

 

Default Method –>

@api.model

def _get_buy_route(self):

buy_route = self.env.ref(‘purchase.route_warehouse0_buy’)

if buy_route:

return buy_route.ids

return []

 

Here,

“route_warehouse0_buy” is the record id of “Buy” from  ‘stock.location.route’  relation which is by default selected.

For further query contact us