在不同模型的树视图中显示one2many字段[Odoo]

问题描述 投票:2回答:1

我想显示特定发票中应用的所有税金及其金额[在模型账户的树形视图中.invoice]这是输出:

enter image description here

“税行”列显示表account.invoice.tax中存在的税金ID(而我想显示其名称和相应金额)

模型account.invoice有一个名为tax_line_ids [Tax Lines]的字段,其中包含发票上所有税的记录,该记录存储在单独的表account.invoice.tax中,该表在其自己的树视图中如下所示:

enter image description here

我想提取税名及其相应金额,以反映在account.invoice的树状视图中

这是我的python代码似乎不起作用:

@api.one
def taxz(self):
    tax_pool = self.pool.get("account.tax")
    found_taxes = tax_pool.read(cr, uid, [tax_id,], ["tax_line_ids"], context)
    found_tax = found_taxes[0] if found_taxes else None
    tax_line_ids = found_tax["tax_line_ids"]
    _logger.critical("context type: " + type(context))
    _logger.critical("context content: " + str(context))
    _logger.critical(tax_line_ids)

视图的xml代码:

<field name="tax_line_ids" widget="many2many_tags" />
python odoo odoo-10
1个回答
0
投票

试试这个:

    class Invoice(models.Model):
        _inherit = 'account.invoice'

        tax_line_ids = fields.Many2many('account.invoice.tax',
                                        'invoice_taxes', 
                                        'invoice_id', 
                                        'taxt_id',
                                        'List of taxes',
                                    compute='get_tax_list', store=True)



        @api.depends('tax_line', 'tax_line.amount')
        def get_tax_list(self):
            for rec in self:
                if rec.taxe_line:
                    rec.tax_line_ids = [(6,0,rec.tax_line.ids)]
                else:
                    rec.tax_line_ids = [(5,0,0)]

但是,如果要显示在name_get中覆盖account.invoice.tax方法所需的金额,这样做只会在树视图中显示不含金额的税单,但这会影响所有x2many字段。

class AcountInvoiceTax(models.Model):

    _inherit = 'account.invoice.tax'

    @api.multi
    def name_get(self):
        res = []
        for rec in self:
            res.append((rec.id, rec.name +': '+ str(rec.amount)))
        return res

如果您不想要这个,那么您需要将类型更改为Char并重新计算字段或创建另一个model以节省税收并为该模型定义name_get

正如你所看到的,这对我有用,如果你仍然得到keyErro你必须做一些错误检查你的代码缩进继承值...:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.