[FIX] website: perf - prefetch every website menus in one go

Without this commit, the website menus would not be prefetch all in once.
Thus, every call to `submenu` template (called recursively) would do SQL
queries. One when accessing website.menu_id, then for every access on menu's
child_ids. It would also make an extra request for groups since the menus have
an ir.rule for that.

with following structure, 13 SQL queries will be avoided, regardless if there is
a group or not on the menus:
- Top menu
  - Home
  - Contact us
    - Sub Contact us
      - Sub Sub Contact us

Related to #47257
task-2211013

X-original-commit: 7fb016f9966bb5e2289ba17eacafb8887626514e
This commit is contained in:
Romain Derie
2020-03-19 15:37:44 +00:00
parent 96486de1c1
commit f95af92ae3
+11 -2
View File
@@ -133,9 +133,18 @@ class Website(models.Model):
self.default_lang_id = language_ids[0]
def _compute_menu(self):
Menu = self.env['website.menu']
for website in self:
website.menu_id = Menu.search([('parent_id', '=', False), ('website_id', '=', website.id)], order='id', limit=1).id
menus = self.env['website.menu'].search([('website_id', '=', website.id)])
# use field parent_id (1 query) to determine field child_id (2 queries by level)"
for menu in menus:
menu._cache['child_id'] = ()
for menu in menus:
# don't add child menu if parent is forbidden
if menu.parent_id and menu.parent_id in menus:
menu.parent_id._cache['child_id'] += (menu.id,)
website.menu_id = menus and menus.filtered(lambda m: not m.parent_id)[0].id or False
@api.model
def create(self, vals):