odoo中的重复记录问题

时间:2019-06-26 11:22:16

标签: python python-3.x odoo odoo-8 odoo-9

我在销售订单行和发票行中添加了新的自定义字段,然后我覆盖了名为action_invoice_create()的方法,该方法在用户单击按钮时触发。

我试图从这种方法中得到的是这样的输出:

order_line SR
order_line ZRE
invoice_line SR
invoice_line ZRE

但是目前我得到的输出显示重复的记录:

order_line SR
invoice_line SR
invoice_line SR
order_line ZRE
invoice_line ZRE
invoice_line ZRE

所以我需要帮助来纠正for循环以获得正确的输出。

class SalesOrder(models.Model):
    _inherit = 'sale.order'
    _description = 'Move VAT Code'

    @api.multi
    def action_invoice_create(self, grouped=False, final=False):
        res = super(SalesOrder, self).action_invoice_create()
        for record in self:
            for rec in record.order_line:
              print('order_line %s' % rec.x_vat_code_id.name)
              for invoice in record.invoice_ids:
                for invoice_line in invoice.invoice_line_ids:
                    invoice_line['vat_code_id'] = rec.x_vat_code_id.id
                    print('invoice_line %s' % rec.x_vat_code_id.name)

        return res

数据格式:

{'order_line': [(0, 0, {'state': 'draft', 'x_qty': 0.0, 'x_partner': False, 'x_price_unit': 0.0, 'x_onhand': 0.0, 'x_reverse': 0.0, 'x_reference': False, 'x_stock_partner': False, 'x_vat_code_id': 51})]}

1 个答案:

答案 0 :(得分:0)

您可以不希望像这样遍历record.invoice_ids的循环,并检查循环是否正常。

class SalesOrder(models.Model):
_inherit = 'sale.order'
_description = 'Move VAT Code'

@api.multi
def action_invoice_create(self, grouped=False, final=False):
    res = super(SalesOrder, self).action_invoice_create()
    for record in self:
        for rec in record.order_line:
          print('order_line %s' % rec.x_vat_code_id.name)
        for invoice in record.invoice_ids:
          for invoice_line in invoice.invoice_line_ids:
              invoice_line['vat_code_id'] = rec.x_vat_code_id.id
              print('invoice_line %s' % rec.x_vat_code_id.name)

    return res