Odoo选择取决于另一个选择

时间:2020-01-28 14:15:28

标签: python odoo selection onchange odoo-13

我的模型如下:

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取决于第一次选择

1 个答案:

答案 0 :(得分:0)

看起来唯一的方法是为每个字段制作3个模型并将其绑定到parent_id和child_ids字段

select * 
  from (select * FROM records) as test
  PIVOT (
  max(value) for
  [Question_ID] in ((SELECT distinct [Question_ID] from records order by 1))) AS PivotTable