Zum Inhalt springen
Menü
Sie müssen registriert sein, um mit der Community zu interagieren.
Diese Frage wurde gekennzeichnet
10 Antworten
4104 Ansichten

Hi all, I'm new to Odoo and working with v11. Currently I got a problem with passing value and calculation.

Here is related coding

.py file:

class costSummary(models.Model):

_name = 'cost.summary'

total = XXXXX

cost_line = fields.One2many('cost.details', inverse_name='cost_id')

class costDetails(models.Model):

_name = 'cost.details'

cost_id = fields.Many2one('cost.summary', ondelete='cascade')

cost_line_id = fields.Char(string="SubExpense")

cost_line_desc = fields.Text(string="SubExpense Description")

cost_line_cost = fields.Float(string='Cost')

xml/view file:

<field name="cost_line_cost" sum="Total cost"/>

Now, I would like to use "total" to store the cost value (the total cost) and display it. But I dont know how to do that. I guess it should be looks like this:

total = fields.Float(store=True, compute='_compute_amount')

@api.depends('cost_line_cost')

def _compute_amount(self):

for rec in self:

rec.total += rec.env['cost.details'].search([]).cost_line_cost

Can anyone please show me how to do it in details? what I missed? any hint will be useful, Thanks a lot!

Avatar
Verwerfen
Autor Beste Antwort

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

Avatar
Verwerfen
Beste Antwort

Hi,

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




Thanks

Avatar
Verwerfen
Autor

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

Make sure you have got that field in the model

Autor

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

Autor

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

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

Autor

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)

Autor

expense_id, I mean cost_id

Autor

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!

Verknüpfte Beiträge Antworten Ansichten Aktivität
3
Mai 24
1579
0
Mai 22
1153
2
Juli 20
9292
2
Juni 20
3354
0
Okt. 19
2836