diff --git a/stock_reserve_rule/models/stock_reserve_rule.py b/stock_reserve_rule/models/stock_reserve_rule.py index add61e828786..e794e999a43f 100644 --- a/stock_reserve_rule/models/stock_reserve_rule.py +++ b/stock_reserve_rule/models/stock_reserve_rule.py @@ -82,7 +82,14 @@ def _constraint_fallback_location_id(self): raise ValidationError(msg) def _rules_for_location(self, location): - return self.search([("location_id", "parent_of", location.id)]) + # We'll typically have a handful of rules, so reading all of them then + # checking if they are a parent location of the location is pretty + # fast. Searching all the parent locations then the rules matching them + # can be much slower if we have many locations. + rules = self.search([]).filtered( + lambda rule: rule.location_id.parent_path.startswith(location.parent_path) + ) + return rules def _eval_rule_domain(self, move, domain): move_domain = [("id", "=", move.id)]