Bỏ qua để đến Nội dung
Menu
Câu hỏi này đã bị gắn cờ
10 Trả lời
4108 Lượt xem

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!

Ảnh đại diện
Huỷ bỏ
Tác giả Câu trả lời hay nhất

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

Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

Hi,

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




Thanks

Ảnh đại diện
Huỷ bỏ
Tác giả

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

Tác giả

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

Tác giả

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,

Tác giả

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)

Tác giả

expense_id, I mean cost_id

Tác giả

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!

Bài viết liên quan Trả lời Lượt xem Hoạt động
3
thg 5 24
1579
0
thg 5 22
1153
2
thg 7 20
9295
2
thg 6 20
3355
0
thg 10 19
2838