Odoo 选择依赖于另一个选择
Odoo Selection depends of another Selection
我有这样的模型:
class MyDict(models.Model):
_name='my_module.my_dict'
field1=fields.Char()
field2=fields.Char()
field3=fields.Char()
数据看起来像:
obj_1 | attr_1 | val_1
obj_1 | attr_2 | val_1
obj_1 | attr_2 | val_2
obj_2 | attr_1 | val_1
obj_2 | attr_1 | val_2
从另一个模型我想一步步使用这个数据
class NewModel(models.Model):
_name='my_module.new_model'
selection1=field.Selection(selection='_get_selection1')
selection2=field.Selection(selection='_get_selection2')
selection3=field.Selection(selection='_get_selection3')
def _get_selection1(self):
my_list = []
selection_list = []
full_list = self.env['my_module.my_dict'].search([])
for record in full_list:
if record.field1 not in my_list:
my_list.append(record.field1)
for list_item in my_list:
selection_list.append((str(list_item), str(list_item)))
return selection_list
第二个选择需要依赖第一个
@api.onchange('selection1')
def _get_selection2(self):
my_list = []
selection_list = []
full_list = self.env['my_module.my_dict'].search([('field1', '=', self.selection1])
for record in full_list:
if record.field2 not in my_list:
my_list.append(record.field2)
for list_item in my_list:
selection_list.append((str(list_item), str(list_item)))
return selection_list
但我发现这里有错误
File "/opt/odoo13/odoo/odoo/models.py", line 5817, in process
if res.get('value'):
AttributeError: 'list' object has no attribute 'get'
如何获得selection_list取决于第一次选择
看起来唯一的方法是为每个字段制作 3 个模型并将其绑定到 parent_id 和 child_ids 字段
class MyDict1(models.Model):
_name='my_module.my_dict1'
name=fields.Char()
child_ids=fields.One2many('my_module.my_dict2', 'parent_id')
class MyDict2(models.Model):
_name='my_module.my_dict2'
name=fields.Char()
parent_id=fields.Many2one('my_module.my_dict1')
child_ids=fields.One2many('my_module.my_dict3', 'parent_id')
我有这样的模型:
class MyDict(models.Model):
_name='my_module.my_dict'
field1=fields.Char()
field2=fields.Char()
field3=fields.Char()
数据看起来像:
obj_1 | attr_1 | val_1
obj_1 | attr_2 | val_1
obj_1 | attr_2 | val_2
obj_2 | attr_1 | val_1
obj_2 | attr_1 | val_2
从另一个模型我想一步步使用这个数据
class NewModel(models.Model):
_name='my_module.new_model'
selection1=field.Selection(selection='_get_selection1')
selection2=field.Selection(selection='_get_selection2')
selection3=field.Selection(selection='_get_selection3')
def _get_selection1(self):
my_list = []
selection_list = []
full_list = self.env['my_module.my_dict'].search([])
for record in full_list:
if record.field1 not in my_list:
my_list.append(record.field1)
for list_item in my_list:
selection_list.append((str(list_item), str(list_item)))
return selection_list
第二个选择需要依赖第一个
@api.onchange('selection1')
def _get_selection2(self):
my_list = []
selection_list = []
full_list = self.env['my_module.my_dict'].search([('field1', '=', self.selection1])
for record in full_list:
if record.field2 not in my_list:
my_list.append(record.field2)
for list_item in my_list:
selection_list.append((str(list_item), str(list_item)))
return selection_list
但我发现这里有错误
File "/opt/odoo13/odoo/odoo/models.py", line 5817, in process
if res.get('value'):
AttributeError: 'list' object has no attribute 'get'
如何获得selection_list取决于第一次选择
看起来唯一的方法是为每个字段制作 3 个模型并将其绑定到 parent_id 和 child_ids 字段
class MyDict1(models.Model):
_name='my_module.my_dict1'
name=fields.Char()
child_ids=fields.One2many('my_module.my_dict2', 'parent_id')
class MyDict2(models.Model):
_name='my_module.my_dict2'
name=fields.Char()
parent_id=fields.Many2one('my_module.my_dict1')
child_ids=fields.One2many('my_module.my_dict3', 'parent_id')