[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
This commit is contained in:
Victor Feyens
2020-11-20 10:04:27 +00:00
parent 8d32f8724e
commit 52cc073a11
3 changed files with 3 additions and 3 deletions
+1 -1
View File
@@ -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."))
+1 -1
View File
@@ -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."))
+1 -1
View File
@@ -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')