From d3bbe14c8df4852f7796669c45ad18341ace3916 Mon Sep 17 00:00:00 2001 From: Victor Feyens Date: Thu, 20 Feb 2020 10:08:57 +0000 Subject: [PATCH] [IMP] product: improve creation performance. The default product category and uom should be fixed on generic undeletable records (in data files). And by caching those default values, we can gain up to 20% time when creating products without providing uom and categories. Also remove the unnecessary defaults on the compute fields, which do the same as the compute (and have no use because it is a non stored field). closes odoo/odoo#45791 Signed-off-by: Raphael Collet (rco) --- addons/product/models/product_template.py | 28 +++++++---------------- 1 file changed, 8 insertions(+), 20 deletions(-) diff --git a/addons/product/models/product_template.py b/addons/product/models/product_template.py index 7a4eb015020..a8c5222b528 100644 --- a/addons/product/models/product_template.py +++ b/addons/product/models/product_template.py @@ -17,27 +17,15 @@ class ProductTemplate(models.Model): _description = "Product Template" _order = "name" + @tools.ormcache() def _get_default_category_id(self): - if self._context.get('categ_id') or self._context.get('default_categ_id'): - return self._context.get('categ_id') or self._context.get('default_categ_id') - category = self.env.ref('product.product_category_all', raise_if_not_found=False) - if not category: - category = self.env['product.category'].search([], limit=1) - if category: - return category.id - else: - err_msg = _('You must define at least one product category in order to be able to create products.') - redir_msg = _('Go to Internal Categories') - raise RedirectWarning(err_msg, self.env.ref('product.product_category_action_form').id, redir_msg) + # Deletion forbidden (at least through unlink) + return self.env.ref('product.product_category_all') + @tools.ormcache() def _get_default_uom_id(self): - return self.env["uom.uom"].search([], limit=1, order='id').id - - def _get_default_weight_uom(self): - return self._get_weight_uom_name_from_ir_config_parameter() - - def _get_default_volume_uom(self): - return self._get_volume_uom_name_from_ir_config_parameter() + # Deletion forbidden (at least through unlink) + return self.env.ref('uom.product_uom_unit') def _read_group_categ_id(self, categories, domain, order): category_ids = self.env.context.get('default_categ_id') @@ -97,11 +85,11 @@ class ProductTemplate(models.Model): volume = fields.Float( 'Volume', compute='_compute_volume', inverse='_set_volume', digits='Volume', store=True) - volume_uom_name = fields.Char(string='Volume unit of measure label', compute='_compute_volume_uom_name', default=_get_default_volume_uom) + volume_uom_name = fields.Char(string='Volume unit of measure label', compute='_compute_volume_uom_name') weight = fields.Float( 'Weight', compute='_compute_weight', digits='Stock Weight', inverse='_set_weight', store=True) - weight_uom_name = fields.Char(string='Weight unit of measure label', compute='_compute_weight_uom_name', readonly=True, default=_get_default_weight_uom) + weight_uom_name = fields.Char(string='Weight unit of measure label', compute='_compute_weight_uom_name') sale_ok = fields.Boolean('Can be Sold', default=True) purchase_ok = fields.Boolean('Can be Purchased', default=True)