Help

2

How to add cities and districts in the private address odoo 12?

Avatar
Muhammad Anees

Hii, 

In the Employee's Private Address window there are Street 2 and City fields. 

When a user select a country, it should show available list of Cities and Districts. 

How to make a drop-down list of cities and change Street 2 to districts drop-down list.

How to convert city char field to selection field to many2one with cities table and same for district.

Thanks,



Avatar
Discard
2 Answers
2
Avatar
Muhammad Anees
Best Answer

Here is how I added Cities and Districts to my private address.

1. Install standard City Addresses module
2. Contacts > Configurations > Countries > select a country to apply cities > Edit > Select Enforce Cities check box
3. On the same page add States
4. On the same page there is Cities link
5. Add your cities under the Country and State
6. to add districts create a new module


class District(models.Model): 
_name = 'res.city.district' _description = 'District' _order = 'name' name = fields.Char("Name", required=True, translate=True)
country_id = fields.Many2one('res.country', string='Country', help='Select Country', ondelete='restrict')
city_id = fields.Many2one('res.city', 'City', ondelete='restrict')

7. Add new view for District and show next to Cities link on country detail page
8. Inherit res.partner and change street2 field type to Many2one

class HrResPartner(models.Model):
_inherit = 'res.partner' street2 = fields.Many2one('res.city.district', string='District') @api.onchange('city_id')
def _onchange_city_id(self):
if self.city_id: self.street2 = False self.state_id = self.city_id.state_id
return {'domain': {'street2': [('city_id', '=', self.city_id.id)]}}
else:
self.street2 = False
self.state_id = False
return {'domain': {'district_id': []}} @api.onchange('country_id')
def _onchange_country_id(self):
if self.country_id and self.country_id != self.state_id.country_id:
self.state_id = False
self.city_id = False
self.street2 = False
@api.onchange('state_id')
def _onchange_state(self):
if self.state_id.country_id:
self.country_id = self.state_id.country_id
self.street2 = False
return {'domain': {'city_id': [('state_id', '=', self.state_id.id)]}}



Avatar
Discard
0
Avatar
Paresh Wagh
Best Answer

Hi Muhammad: You cannot change the definition of existing fields. You will need to define new custom fields, display them in your views and hide the existing fields.

Avatar
Discard