Odoo Aide

0
10 Answers
0
David Yao
26/03/2018 21:45

Hi all, Thx for the help from Niyas Raphy, I have a much clear picture. I just solved this problem and for people who has the similar problem, hope my answer can help you.

There are 3 keys to solve this problem.

First of all, in my case, what I'm trying to get is a recordset and then find the cost value for each record, sum it up and create a field to store its value (not setting up total_cost column for each record). Secondly, of course, using @api.depends rather than @api.onchange  Thirdly, using update method

This is what my code looks like:

@api.depends('cost_line')

def _compute_amount(self):
    for rec in self:

        money = 0

        for details_rec in rec.cost_line:
            if details_rec.cost_line_cost:
                money = money +  details_rec.cost_line_cost

    self.update({ 'total' : money })

2
Avatar

Niyas Raphy

--Niyas Raphy--
15052
| 6 6 9
Calicut, Inde
--Niyas Raphy--

Odoo Techno Functionalist

Feel free to connect :  niyasraphyk@gmail.com

Twitter:https://twitter.com/niyasraphy
LinkedIn: https://www.linkedin.com/in/niyasraphy/
FB:https://www.facebook.com/niyasraphy123

Happy to help :)

Niyas Raphy
19/03/2018 00:24

Hi,

The problem seems with the compute function you have wrote, update it like this,




Thanks

8 Comments
David Yao
19/03/2018 01:17

It seems that it unable to find 'cost_line_cost', it arise a keyerror (KeyError: 'cost_line_cost')

Niyas Raphy
19/03/2018 01:30

Make sure you have got that field in the model

David Yao
19/03/2018 01:49

yes, I have "cost_line_cost " in "costDetails" model, But it is not in the "costSummary" model, does it matter?

David Yao
19/03/2018 02:06

hey, Niyas Raphy, I fixed this keyError by changing @api.depends('cost_line_cost') TO @api.depends('cost_line.cost_line_cost'), but I got a new issue, that is the value is not calculated correctly, all my records showing value 134 at the "total" field/column

Niyas Raphy
19/03/2018 02:47

Yes, it will be like this, you have to add the search condition inside the search,

David Yao
19/03/2018 03:12

For the search domain, I have tried search([('cost_line_cost', '>=', 0)]) OR search([('expense_id', '=', True)]), nothing is changing. And also, my issue is all record's "total" column showing value 134 (even for those don't have "cost_line_cost " value)

David Yao
19/03/2018 03:14

expense_id, I mean cost_id

David Yao
19/03/2018 05:22

hey Niyas Raphy, After I create a new record, the total value becomes 0 (old record's value keeps the same, 134). So, it seems that It goes somewhere else to get the value and doing calculation. but where else it will go? it is defined in the "_compute_amount" method, right? please help me out, thanks!

Poser une question
Writer
Étiquettes
Keep Informed
0 Abonné(e)s
About This Community

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

Register
Odoo Training Center

Access to our E-learning platform and experience all Odoo Apps through learning videos, exercises and Quizz.

Test it now