From 353474fba2f9687c99b0c664639ba9936fea904d Mon Sep 17 00:00:00 2001 From: Adrian Torres Date: Thu, 21 Mar 2019 15:38:11 +0000 Subject: [PATCH] [FIX] sale: do create of sale.order.line in batch With this patch, `sale.order.line` records can now be created in batch, this means that operations such as: - SO copy - SO create + commands - SO import will benefit from this patch in terms of performance. E.g. For a SO of 2k lines, duplicating the SO takes: Before patch: 1515s After patch: 171s Creating an SO with SOL as o2m commands: Before patch: 181s After patch: 43s closes odoo/odoo#32040 Signed-off-by: Raphael Collet (rco) --- addons/sale/models/sale.py | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/addons/sale/models/sale.py b/addons/sale/models/sale.py index c73944cd777..6c698a35169 100644 --- a/addons/sale/models/sale.py +++ b/addons/sale/models/sale.py @@ -1046,20 +1046,23 @@ class SaleOrderLine(models.Model): res[field] = line._fields[field].convert_to_write(line[field], line) return res - @api.model - def create(self, values): - if values.get('display_type', self.default_get(['display_type'])['display_type']): - values.update(product_id=False, price_unit=0, product_uom_qty=0, product_uom=False, customer_lead=0) + @api.model_create_multi + def create(self, vals_list): + for values in vals_list: + if values.get('display_type', self.default_get(['display_type'])['display_type']): + values.update(product_id=False, price_unit=0, product_uom_qty=0, product_uom=False, customer_lead=0) - values.update(self._prepare_add_missing_fields(values)) - line = super(SaleOrderLine, self).create(values) - if line.product_id and line.order_id.state == 'sale': - msg = _("Extra line with %s ") % (line.product_id.display_name,) - line.order_id.message_post(body=msg) - # create an analytic account if at least an expense product - if line.product_id.expense_policy not in [False, 'no'] and not self.order_id.analytic_account_id: - self.order_id._create_analytic_account() - return line + values.update(self._prepare_add_missing_fields(values)) + + lines = super().create(vals_list) + for line in lines: + if line.product_id and line.order_id.state == 'sale': + msg = _("Extra line with %s ") % (line.product_id.display_name,) + line.order_id.message_post(body=msg) + # create an analytic account if at least an expense product + if line.product_id.expense_policy not in [False, 'no'] and not line.order_id.analytic_account_id: + line.order_id._create_analytic_account() + return lines _sql_constraints = [ ('accountable_required_fields',