odooonchangeone2manyodoo-16

Update records on one2many fields in wizard for odoo16


Geting Issue 'TypeError: unhashable type: 'dict' for insert values in one2many field from onchange method in odoo16

My code is below:

class EmployeeAddWizard(models.TransientModel):
    _name = 'employee.add.wizard'

    line_ids = fields.One2many('employee.goal.add.line', 'wizard_id', string="Lines")
    
    @api.onchange('challenge_id', 'employee_id')
    def _onchange_action_goal_add(self):
        r = []
        value = {}
        self.line_ids = {}
        if self.challenge_id and self.employee_id:
            goal_records = self.env['gamification.challenge.line'].search([('challenge_id', '=', self.challenge_id.id)])
            for emp in self.employee_id:
                for line in goal_records:
                    data = {'wizard_id': self.id,  # Other table m2o
                            'goal_definition_id': line.definition_id.id,
                            'goal_rating': 0.0,
                            'goal_target': line.target_goal,
                            'employee_id': emp.id,
                            }
                    r.append(data)
            value.update(records=r)
            self.line_ids = value['records']
            
class GoalLine(models.Model):
    _name = 'employee.goal.add.line'


    wizard_id = fields.Integer()
    goal_definition_id = fields.Many2one('gamification.goal.definition', string='Goal Definition', required=True, ondelete="cascade")
    goal_rating = fields.Float('Rating', required=True)
    goal_target = fields.Float('Target Value ', required=True)
    employee_id = fields.Many2one('res.users', string="Employee", required=True, ondelete="cascade")

Thanks in advance


Solution

  • You passed a list of dicts which is not valid, you need to use special commands

    Example:

    r.append(Command.create(data))
    

    or:

    r.append((0, 0, data))
    

    You can use Command.clear(), to remove previous lines if needed ( self.line_ids = {} should raise an error: ValueError: Wrong value).

    Check this answer