İçereği Atla
Menü
Bu soru işaretlendi
3 Cevaplar
13516 Görünümler

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
Vazgeç

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

En İyi Yanıt

Use the inverse function in the field definition - see documentation

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

Avatar
Vazgeç
En İyi Yanıt

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
Vazgeç

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

En İyi Yanıt

@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
Vazgeç
İlgili Gönderiler Cevaplar Görünümler Aktivite
1
Kas 24
39462
2
Ağu 21
7234
1
Tem 20
9417
1
Ara 19
5123
2
Şub 24
3256