Skip to Content
Menu
This question has been flagged
3 Replies
13493 Views

Hi guys,

I have a compute field(margin). Can any one provide me the Inverse function of that field for making that field editable.Or is there any issue for making the field editable by doing readonly=False

margin = fields.Float(string='Margin', compute='_compute_margin')
@api.depends('price_subtotal', 'cost_amount', 'product_price', 'price_unit')
def _compute_margin(self):
print('---------Computing Margin----------')
for line in self:
line.margin = (line.product_price - (
line.price_subtotal + line.cost_amount)) / line.price_unit

Thanks in advance

Avatar
Discard

did you get any answer for this.
now i'm stuck with this

Best Answer

Use the inverse function in the field definition - see documentation

margin = fields.Float(string='Margin', compute='_compute_margin', inverse='_set_margin')

Avatar
Discard
Best Answer

Hi,

Inverse: The inverse function is mainly used to make the compute field editable. A user can’t change a compute field value.An inverse function can be used to solve this problem.This inverse function can be given along with the compute function.


margin = fields.Float(string='Margin', compute='_compute_margin',inverse='_inverse_margin')


@api.depends('price_subtotal', 'cost_amount', 'product_price', 'price_unit')
def _compute_margin(self):
    print('---------Computing Margin----------')
    for line in self:
        line.margin = (line.product_price - (
                line.price_subtotal + line.cost_amount)) / line.price_unit


@api.depends('price_subtotal', 'cost_amount', 'product_price', 'price_unit', 'margin')
def _inverse_margin(self):
    print('---------Inversing Margin----------')
    for line in self:
            line.price_subtotal = (line.product_price - (line.margin * line.price_unit)) - line.cost_amount


Hope it helps

Avatar
Discard

I have a doubt, if i change price_subtotal is price_unit changes by default

Best Answer

@Zbik So what's the answer? Because you didn't provide one, and let's be honest, documentation is pathetic. I'm trying to do the same for my field, it should be computed while one condition is checked, but if not, then the field should be editable manually. You know how to do this? I have an error that maximum recursion depth exceeded.

@api.depends('product_id')
def _get_note_field(self):
"""Method for get note field from SO.

:return: note
"""
for line in self:
note_from_sale = self.env['sale.order.line'].search([
('product_id', '=', line.product_id.id),
('id', '=', line.sale_line_id.id)
])

if note_from_sale:
if note_from_sale.note:
line.note = note_from_sale.note
else:
line.note = None
else:
line.note = None

def _set_po_note(self):
for line in self:
if not line.order_id.sale_order_id:
line.write({'note': line.note})
Avatar
Discard
Related Posts Replies Views Activity
1
Nov 24
39436
2
Aug 21
7220
1
Jul 20
9401
1
Dec 19
5120
2
Feb 24
3244