From 5fd93fb8d138cb9a650410d49745b0777be43be0 Mon Sep 17 00:00:00 2001 From: "Pedro M. Baeza" Date: Tue, 22 Jun 2021 19:51:33 +0200 Subject: [PATCH] [FIX] base_location: Don't slow down the partner view Using the computed allowed + domain with IN is very slow for loading the partner view, more with the initial set, that gets the whole set of records. Luckily, the answer is the magic operator `=?`, that allows to express a conditional domain that is not applied if the right leaf is False, complying with the intention of the dynamic domain, and even expanding it to more conditions for further assistance. Fixes #1073 --- base_location/__manifest__.py | 2 +- base_location/models/res_partner.py | 16 +++------------- base_location/views/res_partner_view.xml | 4 ++-- 3 files changed, 6 insertions(+), 16 deletions(-) diff --git a/base_location/__manifest__.py b/base_location/__manifest__.py index be7444a75..dfb0b4634 100644 --- a/base_location/__manifest__.py +++ b/base_location/__manifest__.py @@ -4,7 +4,7 @@ { "name": "Location management (aka Better ZIP)", - "version": "14.0.1.0.1", + "version": "14.0.1.1.0", "depends": ["base_address_city", "contacts"], "author": ( "Camptocamp," diff --git a/base_location/models/res_partner.py b/base_location/models/res_partner.py index 7dbec234d..4a6eb3e7e 100644 --- a/base_location/models/res_partner.py +++ b/base_location/models/res_partner.py @@ -9,9 +9,6 @@ from odoo.exceptions import ValidationError class ResPartner(models.Model): _inherit = "res.partner" - allowed_zip_ids = fields.Many2many( - comodel_name="res.city.zip", compute="_compute_allowed_zip_ids" - ) zip_id = fields.Many2one( comodel_name="res.city.zip", string="ZIP Location", @@ -19,7 +16,9 @@ class ResPartner(models.Model): compute="_compute_zip_id", readonly=False, store=True, - domain="[('id', 'in', allowed_zip_ids)]", + domain="[('city_id', '=?', city_id), " + "('city_id.country_id', '=?', country_id), " + "('city_id.state_id', '=?', state_id)]", ) city_id = fields.Many2one( index=True, # add index for performance @@ -34,15 +33,6 @@ class ResPartner(models.Model): ) state_id = fields.Many2one(compute="_compute_state_id", readonly=False, store=True) - @api.depends("city_id") - def _compute_allowed_zip_ids(self): - for record in self: - if record.city_id: - domain = [("city_id", "=", record.city_id.id)] - else: - domain = [] - record.allowed_zip_ids = self.env["res.city.zip"].search(domain) - @api.depends("state_id", "country_id") def _compute_zip_id(self): """Empty the zip auto-completion field if data mismatch when on UI.""" diff --git a/base_location/views/res_partner_view.xml b/base_location/views/res_partner_view.xml index 14288c6c4..2fa118d38 100644 --- a/base_location/views/res_partner_view.xml +++ b/base_location/views/res_partner_view.xml @@ -6,7 +6,7 @@ - + - +