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

When storing a computed field it stores 0 in DB, why?

By
Yakito
on 3/15/15, 12:35 AM 1,211 views

Hello,

I am trying to compute a value for a field and store the value in the DB. If I remove the store=True everything seems fine, but when I add the parameter, isntead of showing the resul from the function I simply get a 0 in all my fields.

Why can this be? Why is adding store=True changing my values to 0?

Any tip will be appreciated,

Thanks

 

EDIT: 

    @api.one
    def _compute_fats(self):

        for record in self:
            fats = 0
            fats_id = record.name.id
            get_detail = self.env['trackfood.foods'].search([['id', '=', fats_id],])
            for record2 in get_detail:
                fats = record2.fats

        self.fats = fats
 

fats = fields.Float(compute='_compute_fats', string="Fat" , store=True)

EDIT 2:

I found out that _compute_fats is not even getting triggered when store=True, when I remove it, it works fine, but I need to store the values in the DB for charting porposes. 

2

Cyril Gaspard (GEM)

--Cyril Gaspard (GEM)--
4136
| 5 6 8
Paris, France
--Cyril Gaspard (GEM)--

Forum contributor since 2008

Cyril Gaspard (GEM)
On 3/15/15, 12:10 PM

fats = fields.Float(compute='_compute_fats', string="Fat" , store=True, default=False)

@api.one

@api.depends('name', 'name.fats')
    def _compute_fats(self):

        self.fats = self.name.fats

Unfortunately the code produces the same result. I even uninstalled the module and installed it again and it looks like _compute_fats , what should I be looking at for the cause of the error? Any idea? Thanks for all your help by the way.

Yakito
on 3/15/15, 12:35 PM

update my code, change '' by False for last line and add default=False to the field, but you should delete column fats update the module and verify.

Cyril Gaspard (GEM)
on 3/15/15, 12:58 PM

Indeed with your edit I was able to make it work. Thanks a lot for your time.

Yakito
on 3/15/15, 8:40 PM

good news, bye

Cyril Gaspard (GEM)
on 3/16/15, 1:51 AM

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: 3/15/15, 12:35 AM
Seen: 1211 times
Last updated: 3/22/15, 12:08 AM