Skip to Content
Menu
Musisz się zarejestrować, aby móc wchodzić w interakcje z tą społecznością.
To pytanie dostało ostrzeżenie
3 Odpowiedzi
13504 Widoki

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

Awatar
Odrzuć

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

Najlepsza odpowiedź

Use the inverse function in the field definition - see documentation

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

Awatar
Odrzuć
Najlepsza odpowiedź

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

Awatar
Odrzuć

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

Najlepsza odpowiedź

@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})
Awatar
Odrzuć
Powiązane posty Odpowiedzi Widoki Czynność
1
lis 24
39448
2
sie 21
7231
1
lip 20
9412
1
gru 19
5121
2
lut 24
3250