Passa al contenuto
Menu
È necessario essere registrati per interagire con la community.
La domanda è stata contrassegnata
3 Risposte
13524 Visualizzazioni

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
Abbandona

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

Risposta migliore

Use the inverse function in the field definition - see documentation

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

Avatar
Abbandona
Risposta migliore

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
Abbandona

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

Risposta migliore

@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
Abbandona
Post correlati Risposte Visualizzazioni Attività
1
nov 24
39471
2
ago 21
7244
1
lug 20
9425
1
dic 19
5125
2
feb 24
3264