Search code examples
pythonodooselectiononchangeodoo-13

Odoo Selection depends of another Selection


I have model like:

class MyDict(models.Model):
_name='my_module.my_dict'

field1=fields.Char()
field2=fields.Char()
field3=fields.Char()

data looks like:

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

From another model I want to use this data step by step

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

the second selection need to depends from the first

@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

but I found Error here

File "/opt/odoo13/odoo/odoo/models.py", line 5817, in process
    if res.get('value'):
AttributeError: 'list' object has no attribute 'get'

how can I get selection_list depends from first selection


Solution

  • Looks like the only way is to make 3 model for every field and bind it whith parent_id and child_ids fields

    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')