From 9535600a89110ad2e90fbea11f31aa81a5d219c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20=28avd=29?= Date: Tue, 8 Jun 2021 16:26:40 +0000 Subject: [PATCH] [FIX] stock_sale: imp lot._compute_sale_order_ids perf. Improve _compute_sale_order_ids performances by saving stock.move.line search results to defaultdict before going through records in self. closes odoo/odoo#72019 X-original-commit: d908351fbaec8900244a82ec3a2d678e279535cb Signed-off-by: William Henrotin --- addons/sale_stock/models/stock.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/addons/sale_stock/models/stock.py b/addons/sale_stock/models/stock.py index 71f4ea96539..5450852abcd 100644 --- a/addons/sale_stock/models/stock.py +++ b/addons/sale_stock/models/stock.py @@ -1,6 +1,8 @@ # -*- coding: utf-8 -*- # Part of Odoo. See LICENSE file for full copyright and licensing details. +from collections import defaultdict + from odoo import api, fields, models, _ @@ -155,14 +157,13 @@ class ProductionLot(models.Model): @api.depends('name') def _compute_sale_order_ids(self): + sale_orders = defaultdict(lambda: self.env['sale.order']) + for move_line in self.env['stock.move.line'].search([('lot_id', 'in', self.ids), ('state', '=', 'done')]): + move = move_line.move_id + if move.picking_id.location_dest_id.usage == 'customer' and move.sale_line_id.order_id: + sale_orders[move_line.lot_id.id] |= move.sale_line_id.order_id for lot in self: - stock_moves = self.env['stock.move.line'].search([ - ('lot_id', '=', lot.id), - ('state', '=', 'done') - ]).mapped('move_id') - stock_moves = stock_moves.search([('id', 'in', stock_moves.ids)]).filtered( - lambda move: move.picking_id.location_dest_id.usage == 'customer' and move.state == 'done') - lot.sale_order_ids = stock_moves.mapped('sale_line_id.order_id') + lot.sale_order_ids = sale_orders[lot.id] lot.sale_order_count = len(lot.sale_order_ids) def action_view_so(self):