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.

1

@api.depends not triggering function

By
Clément TheSecond
on 6/19/15, 5:06 AM 2,257 views

I created a stored (stored=True) computed field.

When  field related to this one are modified, I want to change the value of the field.

So I used @api.depense, as follow :

 
@api.multi @api.depends('lst_price','price','taxes_id','price_extra_ttc','price_extra') def _product_price_ttc(self):
    for record in self:
        record.price_ttc = round(record.lst_price * ( 1 + record.taxes_id.amount),2)

But when one the field of the 'depends' if modified, my function does not fire.

What is wrong with my code ?

1
Clément TheSecond
On 6/19/15, 8:25 AM

Finally the solution was : display the api.depends fields on the same page than price_ttc, so when those field are not on the same page that price_ttc, even if I update them, It's not working. 

I'm a bit desapointed : I thought that api.depends was working everywhere at every moment, but nope. All the fields must be in the same view than the modified one :(

I see... you've used store=True in your computed field?

Temur
on 6/19/15, 8:48 AM

Yes sir ... but indeed, with a non-stored field, the displayed value is the good one.

Clément TheSecond
on 6/19/15, 8:56 AM

Yes, I know about buggy behavior of compute field when store=True. In such a case it may display wrong value in certain conditions, but if you do not set store property then it works correctly(with default value: store=False). I agree with you, implementation of computed field leaves something to be desired if computed fields are stored fields as in your case. As by default store=Fasle for computed fields but you set it to True explicitly, it should be better to include field definition in your question... it's easy to skip extra information in question if provided, while it's harder to guess a missing part. One of the solutions for your case may be to remove store=True, if it does not affect anything else. You also found an interesting way to get it worked. cheers

Temur
on 6/19/15, 10:07 AM
1
Bogdan Lisnenko
On 6/19/15, 7:30 AM

try @api.onchange

it triggers function when fields are changed in gui before writing to database

Does api.onchange works when the computed field is not displayed ? Because, I can modify (for exemple) price_extra in a page, but price_ttc is not on this page. So, is it going to change price_ttc anyway ?

Clément TheSecond
on 6/19/15, 8:11 AM

Yes, it should work. see http://odoo-new-api-guide-line.readthedocs.org/en/latest/decorator.html#api-onchange

Bogdan Lisnenko
on 6/19/15, 9:10 AM
0

Temur

--Temur--
2896
| 6 5 7
Tbilisi, Georgia
--Temur--
Programmer
Temur
On 6/19/15, 7:08 AM

your function (that computes a field) will fire when you access the computed field (either when field is displayed in UI somewhere or included in UI as invisible or you access field from code using "my_browserecord.my_field", etc...) and NOT otherwise. There is nothing wrong with your code, just wrong expectation about when it should be fired.

But when I refresh the page (within access to the computed field), the function does not trigger.

Clément TheSecond
on 6/19/15, 8:09 AM

change one the field of the 'depends' and then refresh a page and function is not fired anyway? htat's your scenario?

Temur
on 6/19/15, 8:15 AM

See answer above ... thank you anyway :)

Clément TheSecond
on 6/19/15, 8:27 AM

it is possible that compute function is not fired if there is a valid cache. if you change any field listed in @api.depends, then cache will be invalidated, so after it if you access the field, function will be fired. Lets say we change 10 times one of the fields listed as dependency and after it we access the computed field once. In this scenario, if computed fields will be implemented as you expect, we'll have 11 call of compute function instead of 1 call. Why compute function should be fired unless it's absolutely necessary?

Temur
on 6/19/15, 8:44 AM

I am having the same issue: last_buy = fields.Date(compute='_get_lastbuys', string = 'Last Buy', store = True) last_pay = fields.Date(compute='_get_lastpays', string = 'Last Pay', store = True) @api.one @api.depends('credit') def _get_lastbuys(self): SaleModel = self.env['sale.order'] parser_model = report_sxw.rml_parse(self.env.cr, self.env.uid, self._name, context=self.env.context) orders = SaleModel.search([('partner_id', '=', self.id), ('state', 'in', ('progress', 'done', 'shipping_except', 'manual','invoice_except'))], order="date_order desc", limit=1) print "================== orders ====================", orders if orders: print "=================== DATE ORDER=================================", orders.date_order self.last_buy = orders.date_order return orders.date_order return @api.one @api.depends('credit') def _get_lastpays(self): SaleModel = self.env['account.voucher'] parser_model = report_sxw.rml_parse(self.env.cr, self.env.uid, self._name, context=self.env.context) vouchers = SaleModel.search([('partner_id', '=', self.id), ('state', '=', 'posted')], order="id desc", limit=1) if vouchers: self.last_pay = vouchers.date return vouchers.date return When I load the module or delete the fields, it will run and populate the DB, but never again, if I remove store=True, and remove api.depends, it will populate perfectly. But I need to search on this field, so added api.depends?

EUGE Consulting, E.R. Spada II
on 8/9/15, 3:48 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

2 follower(s)

Stats

Asked: 6/19/15, 5:06 AM
Seen: 2257 times
Last updated: 6/22/15, 3:00 AM