Загрузить Страна -> Штат -> Городская зависимость в Odoo

Как мы можем создать зависимый выбор Country->State->City в Odoo?

  • Например -
    • Выбранная страна = США
      • который загрузит весь штат США
    • Штат = Техас, Калифорния, Огайо, и т. Д.,
      • Выбранный штат = Техас
      • При выборе штата загружаются все города в выбранном состоянии.
    • Город = Остин, Хьюстон, и т. Д.

Как мы можем достичь этого в Odoo?

Я создал ниже код в model.py

country = fields.Many2one('res.country',string='Country', help='Select Country')
state = fields.Many2one('state.country',string='State', help='Enter State')
city = fields.Char('City', help='Enter City')

Файл view.xml имеет код ниже:

<field name="country"/>
<field name="state"/>
<field name="city"/>

1 ответ

Решение
state_id = fields.Many2one("res.country.state", string='State', help='Enter State', ondelete='restrict')
country_id = fields.Many2one('res.country', string='Country', help='Select Country', ondelete='restrict')    
city = fields.Char('City', help='Enter City')
hide = fields.Boolean(string='Hide', compute="_compute_hide")

# Dependent picklist code to show State based on selected Country E.g India -> Gujarat, Maharastra, Rajasthan, etc..
@api.onchange('country_id')
def _onchange_country_id(self):
    if self.country_id:
        return {'domain': {'state_id': [('country_id', '=', self.country_id.id)]}}
    else:
        return {'domain': {'state_id': []}}

# Show Hide State selection based on Country
@api.depends('country_id')
def _compute_hide(self):
    if self.country_id:
        self.hide = False
    else:
        self.hide = True

# view.xml
<field name="hide" invisible="1"/>
<field name="country_id" options="{'no_quick_create': True, 'no_create_edit' : True}"/>
<field name="state_id" options="{'no_quick_create': True, 'no_create_edit' : True}" attrs="{'invisible':[('hide', '=', True)]}"/>
<field name="city"/>
Другие вопросы по тегам