Skip to Content
Menu
This question has been flagged
2 Replies
20819 Views

Hello, I have Odoo 8.

I have a computed field in a model

    pricelist_version = fields.Many2many('product.pricelist.version', 'pagos_vouchers', 'id_voucher', 'id_pago')
    fue_usado = fields.Boolean(compute='_cuenta_pagos', store=True)

    @api.one
    @api.depends('pricelist_version')
    def _cuenta_pagos(self):
        if(len(self.pricelist_version) > 0):
            self.fue_usado = True
        else:
            self.fue_usado = False

I modify the value of 'pricelist_version' (I can check this because I add a field that shows the pricelist_version asociated with the model in a view) but the _cuenta_pagos method is not triggered and I don't found my mistake. Where is it?

Edit: If I modify the pricelist_version values in the view of the model, the method is triggered. But if I modify this from the pricelist_version view, the method is not triggered.

Edit @Bole:

    pricelist_version = fields.Many2many('product.pricelist.version', 'pagos_vouchers', 'id_voucher', 'id_pago')
    fue_usado = fields.Boolean(compute='_cuenta_pagos', store='_check_to_recompute')

    def _check_to_recompute(self, cr, uid, ids, context=None):
        return [ids]

    @api.one
    @api.depends('pricelist_version')
    def _cuenta_pagos(self):
        if(len(self.pricelist_version) > 0):
            self.fue_usado = True
        else:
            self.fue_usado = False

Avatar
Discard
Best Answer

Parameter store=True defines such behaivour... 
to be precise... it computes the stored value once, store it in db field and never recompute again.. 

In order to test this behaivour, first remove store=True param... and try changing values ( it should recompute on each view reload...)
refer to documentation for more info on subject

 

hope it helps

edit1:

Parameter store = True, can accept a list of ids to recompute.. , so you should make store=_check_to_recompute

and def _check_to_recompute(self,...):

   return [ids]

returning any list of ids to store parameter of computed field will trigger recompute on those fields, and making store=_some_method is basicly store != False , so the data will be store.. .
look at documentation for v7 .. might be more explanational... also look for new api documentation on link above

 

EDIT 2.

Look at account_invoice example.. 
    @api.one
    @api.depends('invoice_line.price_subtotal', 'tax_line.amount')
    def _compute_amount(self):
        self.amount_untaxed = sum(line.price_subtotal for line in self.invoice_line)
        self.amount_tax = sum(line.amount for line in self.tax_line)
        self.amount_total = self.amount_untaxed + self.amount_tax

according to this, _compute amount is triggered whenever invoice_line.price_subtotal, or tax_line.ammount is changed...
maybe go that way.. try to debug/test on account_invoice model for beter comprihesion of this subject

Avatar
Discard
Author

You are right, if I delete store=True the computed field works correctly. But if I remove store=True I can't use the computed field in a domain. How can I do this? Thanks!

Author

@Bole, show my edit

Unfortunatly, i'm still working mainly with v7 api, will ake some private tests and reply as soon as i get some result... i know how to do it old-style api... so that might be a good starting point... ( maybe try fileds.function on v7 api ? ) oll theory here is based on it...

Best Answer

Check the addons/account/account_invoice.py for samples.  You need to specify change in what field in pricelist_version that will trigger the recompute of fue_usado. E.g.  @api.depends('pricelist_version.active').  If you need it to be triggered in every save, try write_uid field (you might need to define write_uid in product.pricelist.version).

Avatar
Discard
Author

It works when I add a pricelist_version, so pricelist_version length is more than 0. This turns 'fue_usado' to true. But when I delete all the pricelist_version objects related to the object, 'fue_usado' still is true. Why?

Have you tried combining both? @api.depends('pricelist_version', 'pricelist_version.active'). Just list down all potential triggers. The part when you remove the last object is because there is no triggering records anymore.

Author

And how can I do to trigger the method when there are not records?

By using a change of another field. The inclusion of 'pricelist_version' should do it because it will trigger the write if there is any change in pricelist_version (i.e. addition, removal, etc.)