Price_unit只读变通方法onchange

时间:2018-08-10 08:50:43

标签: odoo odoo-8 odoo-9

所以我的目标是使销售订单行中的price_unit为只读字段。但是,当您执行此操作时,onchange方法就会出现问题,因此,如果我仅在写入后将属性read_unit添加为只读,则其总为0。

因此,作为解决方法,我制作了onchange方法来存储price_unit值,然后在写入和创建方法中将它们添加到price_unit中。

但是write方法中的val没有price_unit_values,但是同时,我的onchange方法将它们更早地添加到price_unit_values中。

我收到此错误

 if vals['price_unit_values']:
    KeyError: 'price_unit_values'
 <xpath expr="/form/sheet/notebook/page/field/tree/field[@name='price_unit']" position="attributes">
                     <attribute name="readonly">1</attribute>
    </xpath>
class SaleOrderLine(models.Model):
    _inherit = "sale.order.line"

    price_unit_values = fields.Float(string='Price Unit Values', digits=(6, 2), required=False)

@api.onchange('price_unit')
    def _onchange_price_unit(self):
        for rec in self:
            if rec.price_unit:
                rec.price_unit_values = rec.price_unit


@api.multi
    def write(self, vals):
         if vals['price_unit_values']:
             vals['price_unit'] = vals['price_unit_values']
        return super(SaleOrderLine, self).write(vals)

 @api.model
def create(self, vals):
    if vals['price_unit_values']:
        vals['price_unit'] = vals['price_unit_values']
    rec = super(SaleOrderLine, self).create(vals)
    return rec

0 个答案:

没有答案
相关问题