Infinity
Infinity

Reputation: 93

odoo how to update the value of one field by updating the value of other, both field is in different class

I am very new to odoo, I want to update the value of discount_type and discount as I update or change the value of global_discount_type and global_order_discount.How can I achieve this as both variables are from different class. I am unable to do using general python approach. Please guide me.

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')

Do I need to made changes in xml file also? I want to achieve this using onchange.

Upvotes: 1

Views: 4055

Answers (3)

Kenly
Kenly

Reputation: 26678

You need to use use related fields and relate discount_type and discount on purchase.order.line to the purchase.order.

For example:

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

Update discount and discount_type for each line when global_order_discount and global_discount_type respectively:

@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

Upvotes: 1

CZoellner
CZoellner

Reputation: 14768

If you always want the global values reflect to the lines use related fields:

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")

Upvotes: 0

khelili miliana
khelili miliana

Reputation: 3822

This exemple may help,

We use depends in your case (Different model), defining all fields have relation with results and we use onchange in the case of Same model.

@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' 

Upvotes: 0

Related Questions