Search code examples
pythonpython-2.7odooodoo-10

Why my compute field does not work on the reverse side?


I have the computed field x_totalunity which depends on the fields product_uom_qty and x_unitparcarton.

What I want is to be able to write also any value in x_totalunity and that product_uom_qty changes due to this (product_uom_qty = x_totalunity / x_unitparcarton). But this does not work.

This is my code :

@api.depends('product_uom_qty', 'discount', 'price_unit', 'tax_id',
             'x_unitparcarton'  )
def _compute_amount(self):
"""
compute the amounts of the SO line.
"""
for line in self:
    line.x_totalunity = line.product_uom_qty * line.x_unitparcarton

x_totalunity = fields.Float(compute='_compute_amount', string='Total unitéé',
    readonly=False, store=True ,  required=True, copy=True)

Solution

  • First, remove discount, price_unit and tax_id from the @api.depends, as they seem to have no relevance in the computation of x_totalunity.

    Then, I think you are looking for the parameter inverse, to perform some actions in case someone is modifying the computed field by hand:

    @api.multi
    @api.depends('product_uom_qty', 'x_unitparcarton')
    def _compute_x_totalunity(self):
        """Compute the amounts of the SO line."""
        for line in self:
            line.x_totalunity = line.product_uom_qty * line.x_unitparcarton
    
    @api.multi
    def _inverse_x_totalunity(self):
        for line in self:
            line.product_uom_qty = line.x_totalunity / line.x_unitparcarton
    
    x_totalunity = fields.Float(
        compute='_compute_x_totalunity',
        inverse='_inverse_x_totalunity',
        string='Total unitéé',
        required=True,
        readonly=False,
        store=True,
        copy=True,
    )