From a4c6c5b8bfb810033efe75127961d314f451784a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Pigeon?= Date: Fri, 1 Oct 2021 17:28:48 +0200 Subject: [PATCH] [14.0] stock_available: improve search performance --- stock_available/models/product_product.py | 10 +++++++--- stock_available_mrp/models/product_product.py | 2 +- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/stock_available/models/product_product.py b/stock_available/models/product_product.py index d50f5f87e..c03bf72cc 100644 --- a/stock_available/models/product_product.py +++ b/stock_available/models/product_product.py @@ -67,6 +67,9 @@ class ProductProduct(models.Model): "the materials already at hand.", ) + def _get_search_immediately_usable_qty_domain(self): + return [("type", "=", "product")] + @api.model def _search_immediately_usable_qty(self, operator, value): """Search function for the immediately_usable_qty field. @@ -76,9 +79,10 @@ class ProductProduct(models.Model): :param value: str :return: list of tuple (domain) """ - products = self.search([]) - # Force prefetch - products.mapped("immediately_usable_qty") + product_domain = self._get_search_immediately_usable_qty_domain() + products = self.with_context(prefetch_fields=False).search( + product_domain, order="id" + ) product_ids = [] for product in products: if OPERATORS[operator](product.immediately_usable_qty, value): diff --git a/stock_available_mrp/models/product_product.py b/stock_available_mrp/models/product_product.py index 1034ba2b8..e6ee21ede 100644 --- a/stock_available_mrp/models/product_product.py +++ b/stock_available_mrp/models/product_product.py @@ -52,6 +52,7 @@ class ProductProduct(models.Model): for product in product_with_bom: # Need by product (same product can be in many BOM lines/levels) + bom_id = first(product.bom_ids) exploded_components = exploded_boms[product.id] component_needs = product._get_components_needs(exploded_components) if not component_needs: @@ -66,7 +67,6 @@ class ProductProduct(models.Model): for component, need in component_needs.items() ] ) - bom_id = first(product.bom_ids) potential_qty = bom_id.product_qty * components_potential_qty potential_qty = potential_qty > 0.0 and potential_qty or 0.0