From 2072de750424e6c150f5554b7be4e5eec2f9afab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thibault=20Delavall=C3=A9e?= Date: Wed, 7 Jul 2021 09:39:59 +0000 Subject: [PATCH] [IMP] base_address_city: allow to search cities based on zip When having cities with similar name it may quickly be difficult to find the right one. Adding zip code to name search helps finding it more easily. IN order to match this new _name_search we update name_get accordingly and display the zip code between parenthesis. Task ID-2525738 closes odoo/odoo#73359 Signed-off-by: Thibault Delavallee (tde) --- addons/base_address_city/models/res_city.py | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/addons/base_address_city/models/res_city.py b/addons/base_address_city/models/res_city.py index c542a1a3a8e..5f9fce6171c 100644 --- a/addons/base_address_city/models/res_city.py +++ b/addons/base_address_city/models/res_city.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- # Part of Odoo. See LICENSE file for full copyright and licensing details. -from odoo import fields, models +from odoo import api, fields, models class City(models.Model): @@ -14,3 +14,18 @@ class City(models.Model): country_id = fields.Many2one('res.country', string='Country', required=True) state_id = fields.Many2one( 'res.country.state', 'State', domain="[('country_id', '=', country_id)]") + + def name_get(self): + res = [] + for city in self: + name = city.name if not city.zipcode else '%s (%s)' % (city.name, city.zipcode) + res.append((city.id, name)) + return res + + @api.model + def _name_search(self, name='', args=None, operator='ilike', limit=100, name_get_uid=None): + args = list(args or []) + # optimize out the default criterion of ``ilike ''`` that matches everything + if not (name == '' and operator == 'ilike'): + args += ['|', (self._rec_name, operator, name), ('zipcode', operator, name)] + return self._search(args, limit=limit, access_rights_uid=name_get_uid)