2017-05-22 51 views
0

我们如何在Odoo中创建依赖于国家 - >州 - >城市的选择?加载国家 - >州 - > Odoo的城市依赖性

  • 例如 -
    • 所选国家=美国
      • 这将载入美国
      • 的所有状态
    • 状态=得克萨斯州,加利福尼亚州,俄亥俄州,...等等,
      • 选定状态=德州
      • 在选择状态时,它将加载处于选定状态的所有城市。
    • City = Austin,Huston,..等等。

,我们怎样才能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"/> 

回答

0
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"/>