Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

0

Computed field is not triggered

By
José
on 1/20/15, 7:06 AM 2,720 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

1

Bole

--Bole--
2385
| 5 4 6
Zagreb | Karlovac, Croatia
--Bole--

there is only 10 kind of people the ones that understand binary ... and others

Bole
On 1/20/15, 7:22 AM

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

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!

José
on 1/20/15, 7:39 AM

@Bole, show my edit

José
on 1/20/15, 9:20 AM

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...

Bole
on 1/20/15, 9:42 AM
1

Ivan

--Ivan--
3210
| 5 3 6
Jakarta, Indonesia
--Ivan--
Ivan
On 1/20/15, 7:51 AM

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).

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?

José
on 1/20/15, 8:10 AM

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.

Ivan
on 1/20/15, 8:21 AM

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

José
on 1/20/15, 8:53 AM

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.)

Ivan
on 1/20/15, 10:56 PM

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

1 follower(s)

Stats

Asked: 1/20/15, 7:06 AM
Seen: 2720 times
Last updated: 3/16/15, 8:10 AM