odoo如何通过更新另一个字段的值来更新一个字段的值,两个字段都在不同的类中

时间:2018-07-25 12:08:00

标签: python xml odoo odoo-9

我对odoo非常陌生,我想在更新或更改discount_typediscount的值时更新global_discount_typeglobal_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来实现。

3 个答案:

答案 0 :(得分:1)

您需要使用使用相关字段,并将discount_type上的discountpurchase.order.linepurchase.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

答案 1 :(得分:0)

此示例可能会有所帮助,

我们根据您的情况使用(不同的模型),定义所有字段都与结果相关 在相同模型的情况下,我们使用onchange。

@api.depends('order_id.total_discount', 'order_id.global_order_discount')
def _amount_all(self):
    for line in self:
        if line.order_id.total_discount == 'something' :
            line.discount_type = 'somethong1'
            line.discount = 'somethong2' 

答案 2 :(得分:0)

如果您始终希望全局值反映到各行,请使用相关字段:

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

    discount = fields.Float(
        related="order_id.global_discount", string="Discount")
    discount_type = fields.Selection(
        related="order_id.global_discount_type", string="Discount Type")