[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) <rco@openerp.com>
This commit is contained in:
Adrian Torres
2019-03-22 12:08:35 +00:00
parent f15a19d127
commit 353474fba2
+16 -13
View File
@@ -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',