Browse Source

[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
14.0
Pedro M. Baeza 3 years ago
parent
commit
5fd93fb8d1
  1. 2
      base_location/__manifest__.py
  2. 16
      base_location/models/res_partner.py
  3. 4
      base_location/views/res_partner_view.xml

2
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,"

16
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."""

4
base_location/views/res_partner_view.xml

@ -6,7 +6,7 @@
<field name="inherit_id" ref="base.view_partner_form" />
<field name="arch" type="xml">
<field name="city" position="before">
<field name="allowed_zip_ids" invisible="1" />
<field name="city_id" invisible="1" />
<field
name="zip_id"
options="{'create_name_field': 'city', 'no_open': True, 'no_create': True}"
@ -19,7 +19,7 @@
expr="//field[@name='child_ids']/form//field[@name='city']"
position="before"
>
<field name="allowed_zip_ids" invisible="1" />
<field name="city_id" invisible="1" />
<field
name="zip_id"
options="{'create_name_field': 'city', 'no_open': True, 'no_create': True}"

Loading…
Cancel
Save