diff --git a/product_route_profile/models/product_template.py b/product_route_profile/models/product_template.py index b32ef52fc..9bcac2830 100644 --- a/product_route_profile/models/product_template.py +++ b/product_route_profile/models/product_template.py @@ -46,6 +46,8 @@ class ProductTemplate(models.Model): ] def _inverse_route_ids(self): + if self._context.get("skip_inverse_route_ids"): + return profiles = self.env["route.profile"].search([]) for rec in self: for profile in profiles: @@ -61,3 +63,12 @@ class ProductTemplate(models.Model): "name": " / ".join(self.route_ids.mapped("name")), "route_ids": [(6, 0, self.route_ids.ids)], } + + @api.model + def create(self, vals): + route_profile_id = vals.get("route_profile_id", False) + if route_profile_id: + route_profile = self.env["route.profile"].browse(route_profile_id) + vals["route_ids"] = [(6, 0, route_profile.route_ids.ids)] + self = self.with_context(skip_inverse_route_ids=True) + return super(ProductTemplate, self).create(vals) diff --git a/product_route_profile/tests/test_product_route_profile.py b/product_route_profile/tests/test_product_route_profile.py index 18a42ba2e..a45ee6c2f 100644 --- a/product_route_profile/tests/test_product_route_profile.py +++ b/product_route_profile/tests/test_product_route_profile.py @@ -70,3 +70,14 @@ class TestProductRouteProfile(SavepointCase): self.product.with_company(self.env.company).route_ids, self.route_profile_1.route_ids, ) + + def test_3_product_creation_with_route_profile(self): + product = self.env["product.template"].create( + { + "name": "Template 2", + "company_id": False, + "route_profile_id": self.route_profile_1.id, + } + ) + + self.assertEqual(product.route_profile_id.id, self.route_profile_1.id)