Search code examples
odoo

How to change many2many field route_ids value in odoo?


I have been working for my below code for few days, but still can't get it work. What I intended to do is when one select raw-material product type (customized field) in Product Create section, Buy route will be selected. On the other hand, select "Finish Good", Manufacture and MTO route will be selected. The below codes does the half because it doesn't clear the previous selected value when switch from Raw-Material to Finish Good. The previous filled up value still remain. Please help! Thx so much.

@ api.onchange ('custom_product_type')

def _onchange_custom_product_type (self):

    if self.custom_product_type:

        self.warehouse = self.env.ref ('stock.warehouse0')

        route_manufacture = self.env.ref ('stock.warehouse0'). manufacture_pull_id.route_id.id

        route_mto = self.env.ref ('stock.warehouse0'). mto_pull_id.route_id.id

        buy_route = self.env.ref ('stock.warehouse0'). buy_pull_id.route_id.id

         if self.custom_product_type == 'RM':

            self.sale_ok = False

            self.purchase_ok = True

            self.update ({'route_ids': [(6, 0, [buy_route])]})

        elif self.custom_product_type == 'FG' or self.custom_product_type == 'HG':

            self.sale_ok = True

            self.purchase_ok = False

            self.update ({'route_ids': [(6, 0, [route_manufacture, route_mto])]})

Solution

  • Your code should work, but you can try to remove them with (5, ) before adding the new routes:

    self.update({'route_ids': [(5, ), (6, 0, [route_manufacture, route_mto])]})