首页 > 解决方案 > odoo如何通过更新另一个字段的值来更新一个字段的值,两个字段都在不同的类中

问题描述

我对 odoo 很陌生,我想更新 and 的值,discount_type因为discount我更新或更改global_discount_typeand的值。我怎样global_order_discount才能实现这一点,因为这两个变量都来自不同的类。我无法使用一般的 python 方法。请指导我。

class PurchaseOrder(models.Model):
_inherit = "purchase.order"

  total_discount = fields.Monetary(string='Total Discount', store=True, readonly=True, compute='_amount_all',
                                 track_visibility='always')
global_discount_type = fields.Selection([
    ('fixed', 'Fixed'),
    ('percent', 'Percent')
], string="Discount Type", )
global_order_discount = fields.Float(string='Global Discount', store=True, track_visibility='always')

class PurchaseOrderLine(models.Model):
_inherit = "purchase.order.line"

discount = fields.Float(string='Discount', digits=dp.get_precision('Discount'), default=0.0)
discount_type = fields.Selection([
    ('fixed', 'Fixed'),
    ('percent', 'Percent')
], string="Discount Type", default='percent')

我还需要对 xml 文件进行更改吗?我想使用 onchange 来实现这一点。

标签: pythonxmlodooodoo-9

解决方案


您需要使用相关字段discount_type并关联 discountpurchase.order.line.purchase.order

例如:

discount = fields.Float(related="order_id.global_order_discount")

分别更新discountdiscount_type为每一global_order_discountglobal_discount_type

@api.onchange('global_order_discount', 'global_discount_type')
def onchange_field(self):
    for line in self.order_line:
        line.discount = self.global_order_discount
        line.discount_type = self.global_discount_type

推荐阅读