如何在odoo模型中保存只读/可编辑假字段上的值?

Mar*_*elo 2 field model openerp odoo-8

我有一个字段,我想计算它的更改价值,但我不希望用户修改它。

如果我将字段设置为readonly = 1或editable = 0,则不会存储该值。

我正在尝试为发票设置全局折扣

class account_invoice(models.Model):
    _inherit = "account.invoice"

    global_discount_p = fields.Float('Descuento porcentaje')
    global_discount = fields.Float('Descuento')
    neto = fields.Float('Neto')

    @api.one
    @api.depends('invoice_line.price_subtotal', 'tax_line.amount','global_discount_p')
    def _compute_amount(self):
        ret = super(account_invoice,self)._compute_amount()
        if self.type == 'in_invoice':
            self.neto = self.amount_untaxed
            discount = self.global_discount_p/100
            self.global_discount = self.neto * discount
            self.amount_untaxed = self.neto - self.global_discount

            for line in self.tax_line:
                line.base = self.amount_untaxed
                line.amount = line.amount - (line.amount * discount)

            self.amount_tax = sum(line.amount for line in self.tax_line)
            self.amount_total = self.amount_untaxed + self.amount_tax

        return ret
Run Code Online (Sandbox Code Playgroud)

并在布局中:

<xpath expr="//field[@name='amount_untaxed']" position="before">
                <field name="neto" readonly="1"/>
                <field name="global_discount_p" onchange="_compute_amount"/>
                <field name="global_discount" readonly="1"/>
            </xpath>
Run Code Online (Sandbox Code Playgroud)

如果我从字段中删除readonly =“ 1” attrs,它将很好地工作

小智 7

在xml文件中odoo11可以使用force_save="1"

希望对你有帮助!!