V10 Passing One2Many values into Transient Model One2Many (TypeError from onchange & DataError from default)

348 Views Asked by At

I'm customizing a module for hr.payroll that registers payment for the payslip, starting with a Register Payment wizard that then passes the data to account.payment when validated for the check to be printed. All other fields like memo and partner_id pass as expected, but doesn't fill anything in my one2many field, which is trying to pull the Name and Amount values from hr.payslip line_ids.

My One2Many field:

pay_line_ids = fields.One2many('pay.line.ids', 'payline_id', string='Paylines', readonly=False, copy=True)

class PayLineIds(models.Model):
    _name = 'pay.line.ids'
    _description = 'Payslip Payment Lines'

    payline_id = fields.Many2one('account.payment', string='Pay Slip', required=False)
    name = fields.Char(string='Name', required=False)
    amount = fields.Float(digits=dp.get_precision('Payroll'))

I've attempted two methods. Any help is appreciated.

First is setting onchange

@api.onchange('pay_line_ids')
def _onchange_lines(self):
    context = dict(self._context or {})
    active_ids = context.get('active_ids', [])
    payslip = self.env['hr.payslip'].browse(active_ids)
    ids = []
    for line in payslip.line_ids:
        lines = {'name':line.name,'amount':line.amount}
        ids.append(lines)
    self.pay_line_ids = [(6,0,ids)]

Which results in the following error:

  File "/mnt/extra-addons/hr_payroll_payment/hr_payroll_register_payment.py", line 119, in _onchange_lines
    self.pay_line_ids = [(6,0,ids)]
  File "/usr/lib/python2.7/dist-packages/odoo/fields.py", line 933, in __set__
    value = self.convert_to_cache(value, record)
  File "/usr/lib/python2.7/dist-packages/odoo/fields.py", line 2055, in convert_to_cache
    ids = OrderedSet(command[2])
  File "/usr/lib/python2.7/dist-packages/odoo/tools/misc.py", line 1121, in __init__
    self._map = OrderedDict((elem, None) for elem in elems)
  File "/usr/lib/python2.7/collections.py", line 52, in __init__
    self.__update(*args, **kwds)
  File "/usr/lib/python2.7/_abcoll.py", line 567, in update
    self[key] = value
  File "/usr/lib/python2.7/collections.py", line 58, in __setitem__
    if key not in self:
TypeError: unhashable type: 'dict'

Second is setting the default.

pay_line_ids = fields.One2many('pay.line.ids', 'payline_id', string='Paylines', readonly=False, copy=True, default=_default_line_ids)

@api.model
def _default_line_ids(self):
    context = dict(self._context or {})
    active_ids = context.get('active_ids', [])
    payslip = self.env['hr.payslip'].browse(active_ids)
    res = {}
    for line in payslip.line_ids:
        res = {
            'name': line.name,
            'amount': line.amount,
            }
        return [(6, 0, res)]

Which results in the following error:

DataError: invalid input syntax for integer: "amount"
LINE 2: ...                      WHERE "pay_line_ids".id IN ('amount', ...
                                                             ^
2

There are 2 best solutions below

1
On

The syntax used to pass values is not correct.

(6, _, ids) replaces all existing records in the set by the ids list

Refer to the documentation in the source code or a similar answer at Set default values for One2many field

0
On

Your res variable contains type dict but

[(6, 0, variable_ids)]  >> here variable_ids must contains list of ids.

If I'm guessing correct you need to link all lines to pay_line_ids.

So don't use [(6, 0, variable_ids)],

Use: [(0, 0, { res })]

and also update your res with 'payline_id' : your_payline_id to link that id with your table.