From 52cc073a1151584fa43687191cd9ea4b2db20e47 Mon Sep 17 00:00:00 2001 From: Victor Feyens Date: Thu, 22 Oct 2020 16:02:48 +0000 Subject: [PATCH] [IMP] *: improve python constraints performance * Do not load x records from database when one is enough. Earlier SQL returns when a matching record is found. * any() can receive a generator as argument Do not explicitly cast a generator as list when it is not needed. Task Id: 2328664 COM PR: https://github.com/odoo/odoo/pull/55525 ENT PR: https://github.com/odoo/enterprise/pull/12250 --- addons/point_of_sale/models/pos_config.py | 2 +- addons/point_of_sale/models/pos_order.py | 2 +- addons/stock/models/stock_move_line.py | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/addons/point_of_sale/models/pos_config.py b/addons/point_of_sale/models/pos_config.py index e39c4f4614c..f361601efa1 100644 --- a/addons/point_of_sale/models/pos_config.py +++ b/addons/point_of_sale/models/pos_config.py @@ -322,7 +322,7 @@ class PosConfig(models.Model): @api.constrains('cash_control') def _check_session_state(self): - open_session = self.env['pos.session'].search([('config_id', '=', self.id), ('state', '!=', 'closed')]) + open_session = self.env['pos.session'].search([('config_id', 'in', self.ids), ('state', '!=', 'closed')], limit=1) if open_session: raise ValidationError(_("You are not allowed to change the cash control status while a session is already opened.")) diff --git a/addons/point_of_sale/models/pos_order.py b/addons/point_of_sale/models/pos_order.py index a570c6bff22..7727bd202b8 100644 --- a/addons/point_of_sale/models/pos_order.py +++ b/addons/point_of_sale/models/pos_order.py @@ -928,7 +928,7 @@ class AccountCashRounding(models.Model): @api.constrains('rounding', 'rounding_method', 'strategy') def _check_session_state(self): - open_session = self.env['pos.session'].search([('config_id.rounding_method', '=', self.id), ('state', '!=', 'closed')]) + open_session = self.env['pos.session'].search([('config_id.rounding_method', 'in', self.ids), ('state', '!=', 'closed')], limit=1) if open_session: raise ValidationError( _("You are not allowed to change the cash rounding configuration while a pos session using it is already opened.")) diff --git a/addons/stock/models/stock_move_line.py b/addons/stock/models/stock_move_line.py index 19825276ec3..404a79dd909 100644 --- a/addons/stock/models/stock_move_line.py +++ b/addons/stock/models/stock_move_line.py @@ -107,7 +107,7 @@ class StockMoveLine(models.Model): @api.constrains('qty_done') def _check_positive_qty_done(self): - if any([ml.qty_done < 0 for ml in self]): + if any(ml.qty_done < 0 for ml in self): raise ValidationError(_('You can not enter negative quantities.')) @api.onchange('product_id', 'product_uom_id')