Skip ke Konten
Menu
Pertanyaan ini telah diberikan tanda
3 Replies
13492 Tampilan

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
Buang

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

Jawaban Terbai

Use the inverse function in the field definition - see documentation

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

Avatar
Buang
Jawaban Terbai

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
Buang

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

Jawaban Terbai

@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
Buang
Post Terkait Replies Tampilan Aktivitas
1
Nov 24
39436
2
Agu 21
7219
1
Jul 20
9401
1
Des 19
5120
2
Feb 24
3244