From 7f218598fe5c21ace05d7aa2063d2ae4e4f8dc13 Mon Sep 17 00:00:00 2001 From: "Xavier BOL (xbo)" Date: Mon, 17 Jan 2022 16:18:56 +0100 Subject: [PATCH] [IMP] project_purchase: improve performance for `purchase_orders_count` Before this commit, when the `purchase_orders_count` field is computed, we do a search in `purchase.order.line` to get the purchase order linked to the projects in `self` of the `_compute_purchase_orders_count` method. However, if no project has AA set this search is useless, we can directly set `purchase_orders_count` to 0 for all projects because the linked between the POs and the projects is made with AA of the project. This commit changes the method to search the PO linked to the projects only when at least one project has AA set, otherwise we directly set the `purchase_orders_count` to 0 for all projects contained in `self`. task-2710808 --- addons/project_purchase/models/project.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/addons/project_purchase/models/project.py b/addons/project_purchase/models/project.py index 6bf9a462cea..7f7344eb350 100644 --- a/addons/project_purchase/models/project.py +++ b/addons/project_purchase/models/project.py @@ -11,8 +11,10 @@ class Project(models.Model): @api.depends('analytic_account_id') def _compute_purchase_orders_count(self): + if not self.analytic_account_id: + self.purchase_orders_count = 0 + return purchase_orders_data = self.env['purchase.order.line']._read_group([ - ('account_analytic_id', '!=', False), ('account_analytic_id', 'in', self.analytic_account_id.ids) ], ['account_analytic_id', 'order_id:count_distinct'], ['account_analytic_id']) mapped_data = dict([(data['account_analytic_id'][0], data['order_id']) for data in purchase_orders_data])