0

我正在为 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', ...
                                                             ^
4

2 回答 2

0

您的 res 变量包含类型dict

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

如果我猜对了,您需要将所有行链接到pay_line_ids.

所以不要使用[(6, 0, variable_ids)]

利用: [(0, 0, { res })]

并更新您的 res'payline_id' : your_payline_id以将该 id 与您的表链接。

于 2020-06-07T08:01:49.557 回答
0

用于传递值的语法不正确。

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

请参阅源代码中的文档或设置 One2many 字段的默认值中的类似答案

于 2020-04-04T22:44:01.363 回答