我正在为 hr.payroll 定制一个模块,该模块注册工资单的付款,从注册付款向导开始,然后在验证要打印的支票时将数据传递给 account.payment。memo 和 partner_id 等所有其他字段按预期通过,但在我的 one2many 字段中没有填写任何内容,该字段试图从 hr.payslip line_ids 中提取名称和金额值。
我的 One2Many 字段:
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'))
我尝试了两种方法。任何帮助表示赞赏。
首先是设置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)]
这会导致以下错误:
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'
二是设置默认值。
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)]
这会导致以下错误:
DataError: invalid input syntax for integer: "amount"
LINE 2: ... WHERE "pay_line_ids".id IN ('amount', ...
^