python - account.invoice 添加自定义计算和过滤字段

标签 python odoo odoo-8 odoo-9

对不起我的英语

我正在编写一个自定义 Odoo 模块,我的目标是在 account.invoice 中添加一个自定义计算字段,其中包含存储在tax_line_ids amount 字段中的每个税值的总和(不包括负预扣税);这是我的代码:

# -*- coding: utf-8 -*-
from openerp import models, fields, api

class account_invoice(models.Model):
    _inherit = 'account.invoice'
    x_sum_stored_taxes_exclude_withholding = fields.Float('Total Taxes', compute='_compute_total_taxes', digits=(12,2), store=True)

    @api.one
    @api.depends('tax_line_ids.amount')
    def _compute_total_taxes(self):
        for record in self:
                record.x_sum_stored_taxes_exclude_withholding = sum(line.amount for line in record.x_sum_stored_taxes_exclude_withholding)

但是新字段“x_sum_stored_taxes_exclude_withholding”中的结果仅用零填充。我真的尝试了很多方法,但找不到合适的!

救命!! :'(

最佳答案

record.x_sum_stored_taxes_exclude_withholding =\
     sum(line.amount for line in record.x_sum_stored_taxes_exclude_withholding)

您应该使用“tax_line_ids”?

record.x_sum_stored_taxes_exclude_withholding =\
     sum([line.amount for line in record.tax_line_ids])

当然你只需要正值:

record.x_sum_stored_taxes_exclude_withholding =\
     sum([line.amount for line in record.tax_line_ids if line.amount >= 0.0])

关于python - account.invoice 添加自定义计算和过滤字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39341727/

相关文章:

python - 属性错误: 'list' object has no attribute 'order_by' Python

javascript - Odoo - 在特定 View 上自动刷新页面

javascript - 如何在 Odoo 9 中执行 JS 文件中的函数?

python - 如何在odoo中的另一个模型中创建记录的同时在模型中创建记录

python - Pandas groupby 在保留多个聚合的组内进行排序,并使用构面将其可视化

python - 有没有办法在gpu计算的时候并行加载数据?

python - python 中的 type 和 type.__new__ 有什么区别?

python - OpenERP - 不同 View 页面中字段的 onchange 方法?

python - Odoo qweb调用python方法

Odoo - 覆盖默认公司 View