存储值 (onchange) - 新 api [Odoo/Openerp]
Store value (onchange) - new api [Odoo/Openerp]
我在 'sale.order.line' 对象中添加了 2 个字段。假设 'field1' 和 'field2',它们是只读字段。每当订单行中的产品更改时,都会显示这 2 个字段的值。
当我select一个产品时,它显示了两个字段的值,但是当保存它时,值将返回0,不存储。
这是我的代码:
class sale_order_line(models.Model):
_inherit = 'sale.order.line'
field1 = fields.Float('One')
field2 = fields.Float('Two')
@api.multi
def product_id_change(self, pricelist, product, qty=0,
uom=False, qty_uos=0, uos=False, name='', partner_id=False,
lang=False, update_tax=True, date_order=False, packaging=False, fiscal_position=False, flag=False):
res = super(sale_order_line, self).product_id_change(pricelist, product, qty,
uom, qty_uos, uos, name, partner_id,
lang, update_tax, date_order, packaging, fiscal_position, flag)
if product:
one = self.search([('product_id', '=', product), ('partner_id', '=', partner_id)])
two = self.search([('product_id', '=', product), ('partner_id', '!=', partner_id)])
if customer:
field1 = one[-1]
res['value']['field1'] = field1
if other:
field2 = two[-1].
res['value']['field2'] = field2
return res
因为readonly
模式只对显示有效。在这种情况下,字段中的值不会发送到服务器端。
您可以覆盖 sale.order.line
的方法 create
。它应该是这样的:
class sale_order_line(models.Model):
_inherit = 'sale.order.line'
@api.model
def create(self, vals):
# just example
vals[u'field1'] = 2.03
vals[u'field2'] = 3.05
return super(sale_order_line, self).create(vals)
希望对您有所帮助。
在 Odoo 框架中,我们现在允许设置只读字段值,并且只读字段不会在 vals 中进入 create 和 write 方法。
因此,如果您在 onchange 方法中设置那些只读字段值,那么它也不会保留它的值,因为它本质上是只读的,不会给您任何错误。
Purpose : The aims behind to define readonly attributes is to behave same through the all states of the record on UI and user can not change it's value and mainly define for display purpose.That is why readonly fields are not accessible for edit in onchange method.
解决方案:
您需要按以下方式覆盖 CREATE 或 WRITE 方法。
@api.model
def create(self, vals):
######
# WRITE YOUR LOGIC HERE AND BRING THOSE VALUE INTO VARIABLE AND THEN UPDATE IT IN VALS
VARIABLE_1 = SEARCH YOUR DATA
VARIABLE_2 = SEARCH YOUR DATA
vals.update({field1' : VARIABLE_1, 'field_2' : VARIABLE_2})
return super(sale_order_line, self).create(vals)
通过在调用 super 方法之前将那些只读字段设置到字典中来更新 vals(记录字典),或者在调用 super 方法之后更新这些字段。
您的问题有替代解决方案。在 Odoo 应用程序中,该系统可用的一个模块将在数据库中存储只读值。
我在 'sale.order.line' 对象中添加了 2 个字段。假设 'field1' 和 'field2',它们是只读字段。每当订单行中的产品更改时,都会显示这 2 个字段的值。
当我select一个产品时,它显示了两个字段的值,但是当保存它时,值将返回0,不存储。
这是我的代码:
class sale_order_line(models.Model):
_inherit = 'sale.order.line'
field1 = fields.Float('One')
field2 = fields.Float('Two')
@api.multi
def product_id_change(self, pricelist, product, qty=0,
uom=False, qty_uos=0, uos=False, name='', partner_id=False,
lang=False, update_tax=True, date_order=False, packaging=False, fiscal_position=False, flag=False):
res = super(sale_order_line, self).product_id_change(pricelist, product, qty,
uom, qty_uos, uos, name, partner_id,
lang, update_tax, date_order, packaging, fiscal_position, flag)
if product:
one = self.search([('product_id', '=', product), ('partner_id', '=', partner_id)])
two = self.search([('product_id', '=', product), ('partner_id', '!=', partner_id)])
if customer:
field1 = one[-1]
res['value']['field1'] = field1
if other:
field2 = two[-1].
res['value']['field2'] = field2
return res
因为readonly
模式只对显示有效。在这种情况下,字段中的值不会发送到服务器端。
您可以覆盖 sale.order.line
的方法 create
。它应该是这样的:
class sale_order_line(models.Model):
_inherit = 'sale.order.line'
@api.model
def create(self, vals):
# just example
vals[u'field1'] = 2.03
vals[u'field2'] = 3.05
return super(sale_order_line, self).create(vals)
希望对您有所帮助。
在 Odoo 框架中,我们现在允许设置只读字段值,并且只读字段不会在 vals 中进入 create 和 write 方法。
因此,如果您在 onchange 方法中设置那些只读字段值,那么它也不会保留它的值,因为它本质上是只读的,不会给您任何错误。
Purpose : The aims behind to define readonly attributes is to behave same through the all states of the record on UI and user can not change it's value and mainly define for display purpose.That is why readonly fields are not accessible for edit in onchange method.
解决方案:
您需要按以下方式覆盖 CREATE 或 WRITE 方法。
@api.model
def create(self, vals):
######
# WRITE YOUR LOGIC HERE AND BRING THOSE VALUE INTO VARIABLE AND THEN UPDATE IT IN VALS
VARIABLE_1 = SEARCH YOUR DATA
VARIABLE_2 = SEARCH YOUR DATA
vals.update({field1' : VARIABLE_1, 'field_2' : VARIABLE_2})
return super(sale_order_line, self).create(vals)
通过在调用 super 方法之前将那些只读字段设置到字典中来更新 vals(记录字典),或者在调用 super 方法之后更新这些字段。
您的问题有替代解决方案。在 Odoo 应用程序中,该系统可用的一个模块将在数据库中存储只读值。