Before this commit, creating a record of a model with at least one property/company_dependent field would trigger the field's inverse method regardless of whether an actual value was being passed in for the field or not. This means that if no value was given or if the value was the same as the default value, we would waste precious time in the property field's inverse method. With this commit, we do not call the inverse method if: 1) there is no value for it in the vals dict (use default) 2) the value in the vals dict is the same as the default value As an example, when creating a res.partner record while having account installed (which introduces a property field in res.partner), the creation time goes from 24ms to 14ms. closes odoo/odoo#36267 Signed-off-by: Raphael Collet (rco) <rco@openerp.com>
63 lines
1.8 KiB
Python
63 lines
1.8 KiB
Python
# -*- coding: utf-8 -*-
|
|
# Part of Odoo. See LICENSE file for full copyright and licensing details.
|
|
|
|
from odoo import models, fields, api, tools
|
|
|
|
|
|
class BaseModel(models.Model):
|
|
_name = 'test_performance.base'
|
|
_description = 'Test Performance Base'
|
|
|
|
name = fields.Char()
|
|
value = fields.Integer(default=0)
|
|
value_pc = fields.Float(compute="_value_pc", store=True)
|
|
partner_id = fields.Many2one('res.partner', string='Customer')
|
|
|
|
line_ids = fields.One2many('test_performance.line', 'base_id')
|
|
total = fields.Integer(compute="_total", store=True)
|
|
tag_ids = fields.Many2many('test_performance.tag')
|
|
|
|
@api.depends('value')
|
|
def _value_pc(self):
|
|
for record in self:
|
|
record.value_pc = float(record.value) / 100
|
|
|
|
@api.depends('line_ids.value')
|
|
def _total(self):
|
|
for record in self:
|
|
record.total = sum(line.value for line in record.line_ids)
|
|
|
|
|
|
class LineModel(models.Model):
|
|
_name = 'test_performance.line'
|
|
_description = 'Test Performance Line'
|
|
|
|
base_id = fields.Many2one('test_performance.base', required=True, ondelete='cascade')
|
|
value = fields.Integer()
|
|
|
|
def init(self):
|
|
# line values should be unique per "base" - useful for testing corner cases with unique lines
|
|
tools.create_unique_index(self._cr, 'test_performance_line_uniq', self._table, ['base_id', 'value'])
|
|
|
|
|
|
class TagModel(models.Model):
|
|
_name = 'test_performance.tag'
|
|
_description = 'Test Performance Tag'
|
|
|
|
name = fields.Char()
|
|
|
|
|
|
class Bacon(models.Model):
|
|
_name = 'test_performance.bacon'
|
|
_description = 'Test Performance Bacon'
|
|
|
|
property_eggs = fields.Many2one(
|
|
'test_performance.eggs', company_dependent=True, string='Eggs')
|
|
|
|
|
|
class Eggs(models.Model):
|
|
_name = 'test_performance.eggs'
|
|
_description = 'Test Performance Eggs'
|
|
|
|
name = fields.Char()
|