如何在 odoo 模型中保存只读/可编辑错误字段的值?
How to save value on Readonly / editable false fields in odoo Models?
我有一个字段,我想计算它的更改值,但我不希望用户可以修改它。
如果我将字段设置为 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
并且在布局中:
<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>
如果我从字段中删除 readonly="1" 属性,效果很好
尝试将字段定义为
neto = fields.Float('Neto', compute='_compute_amount', store=True)
global_discount = fields.Float('Descuento', compute='_compute_amount', store=True)
在您的 account_invoice class 中覆盖这两个方法并声明字段,如
global_discount = fields.Float('Descuento', readonly=True)
@api.model
def create(self, vals):
if vals['global_discount']:
if vals['type'] == 'in_invoice':
vals['neto'] = vals['amount_untaxed']
discount = vals['global_discount_p']/100
vals['global_discount'] = vals['neto'] * discount
vals['amount_untaxed'] = vals['neto'] - vals['global_discount']
for line in vals['tax_line']:
line.base = vals['amount_untaxed']
line.amount = line.amount - (line.amount * discount)
vals['amount_tax'] = sum(line.amount for line in vals['tax_line'])
vals['amount_total'] = vals['amount_untaxed'] + vals['amount_tax']
res = super(PersonInformation, self).create(vals)
return res
@api.multi
def write(self, vals):
if vals['global_discount']:
if vals['type'] == 'in_invoice':
vals['neto'] = vals['amount_untaxed']
discount = vals['global_discount_p']/100
vals['global_discount'] = vals['neto'] * discount
vals['amount_untaxed'] = vals['neto'] - vals['global_discount']
for line in vals['tax_line']:
line.base = vals['amount_untaxed']
line.amount = line.amount - (line.amount * discount)
vals['amount_tax'] = sum(line.amount for line in vals['tax_line'])
vals['amount_total'] = vals['amount_untaxed'] + vals['amount_tax']
res = super(PersonInformation, self).create(vals)
return res
并且在布局中:
<field name="global_discount" readonly="1"/>
希望对您有所帮助。
在 odoo11
中,您可以在 xml 文件中使用 force_save="1"
希望对您有所帮助!!
我有一个字段,我想计算它的更改值,但我不希望用户可以修改它。
如果我将字段设置为 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
并且在布局中:
<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>
如果我从字段中删除 readonly="1" 属性,效果很好
尝试将字段定义为
neto = fields.Float('Neto', compute='_compute_amount', store=True)
global_discount = fields.Float('Descuento', compute='_compute_amount', store=True)
在您的 account_invoice class 中覆盖这两个方法并声明字段,如
global_discount = fields.Float('Descuento', readonly=True)
@api.model
def create(self, vals):
if vals['global_discount']:
if vals['type'] == 'in_invoice':
vals['neto'] = vals['amount_untaxed']
discount = vals['global_discount_p']/100
vals['global_discount'] = vals['neto'] * discount
vals['amount_untaxed'] = vals['neto'] - vals['global_discount']
for line in vals['tax_line']:
line.base = vals['amount_untaxed']
line.amount = line.amount - (line.amount * discount)
vals['amount_tax'] = sum(line.amount for line in vals['tax_line'])
vals['amount_total'] = vals['amount_untaxed'] + vals['amount_tax']
res = super(PersonInformation, self).create(vals)
return res
@api.multi
def write(self, vals):
if vals['global_discount']:
if vals['type'] == 'in_invoice':
vals['neto'] = vals['amount_untaxed']
discount = vals['global_discount_p']/100
vals['global_discount'] = vals['neto'] * discount
vals['amount_untaxed'] = vals['neto'] - vals['global_discount']
for line in vals['tax_line']:
line.base = vals['amount_untaxed']
line.amount = line.amount - (line.amount * discount)
vals['amount_tax'] = sum(line.amount for line in vals['tax_line'])
vals['amount_total'] = vals['amount_untaxed'] + vals['amount_tax']
res = super(PersonInformation, self).create(vals)
return res
并且在布局中:
<field name="global_discount" readonly="1"/>
希望对您有所帮助。
在 odoo11
中,您可以在 xml 文件中使用 force_save="1"
希望对您有所帮助!!