[IMP] website_sale: speed up public category model

Speed up:
- all queries involving `child_of` and `parent_of` by using
	´_parent_store=True` and `parent_path`
- `name_get` by also taking advantage of `parent_path`
- `order by` by indexing the sequence

This reduces the time of the corresponding search lines by 20%.

PR: #34249
This commit is contained in:
Sébastien Theys
2019-07-17 15:55:29 +00:00
parent 46be94e9d3
commit 21df20630c
+12 -7
View File
@@ -139,12 +139,15 @@ class ProductPublicCategory(models.Model):
_name = "product.public.category"
_inherit = ["website.seo.metadata", "website.multi.mixin"]
_description = "Website Product Category"
_parent_store = True
_order = "sequence, name"
name = fields.Char(required=True, translate=True)
parent_id = fields.Many2one('product.public.category', string='Parent Category', index=True)
parent_path = fields.Char(index=True)
child_id = fields.One2many('product.public.category', 'parent_id', string='Children Categories')
sequence = fields.Integer(help="Gives the sequence order when displaying a list of product categories.")
parents_and_self = fields.Many2many('product.public.category', compute='_compute_parents_and_self')
sequence = fields.Integer(help="Gives the sequence order when displaying a list of product categories.", index=True)
# NOTE: there is no 'default image', because by default we don't show
# thumbnails for categories. However if we have a thumbnail for at least one
# category, then we display a default image on the other, so that the
@@ -178,14 +181,16 @@ class ProductPublicCategory(models.Model):
def name_get(self):
res = []
for category in self:
names = [category.name]
parent_category = category.parent_id
while parent_category:
names.append(parent_category.name)
parent_category = parent_category.parent_id
res.append((category.id, ' / '.join(reversed(names))))
res.append((category.id, " / ".join(category.parents_and_self.mapped('name'))))
return res
def _compute_parents_and_self(self):
for category in self:
if category.parent_path:
category.parents_and_self = self.env['product.public.category'].browse([int(p) for p in category.parent_path.split('/')[:-1]])
else:
category.parents_and_self = category
class ProductTemplate(models.Model):
_inherit = ["product.template", "website.seo.metadata", 'website.published.multi.mixin', 'rating.mixin']