Odoo Help

0

Compute field V8

By
Helmi Dhaoui
on 7/24/15, 12:02 PM 5,607 views

I have to set a field that compute the difference between the old and the new value of a float field, I defined an auxiliary field that contain the old value, but the problem that the value of auxiliary field replaced by the new value then the diff always Zero.

   @api.multi
def _computeVar(self): for record in self: 
            record .val_aux= record .val
            record .val_diff= record .val-record.val_aux

    val=fields.Float(string='val')
val_aux=fields.Float(string='val aux',compute='_computediff', readonly=True) val_diff= fields.Float(string='val diff',compute='_computediff', readonly=True)


 And when wereplace the order of line of the fuction we will have a problem : field used before calculated.
How to solve this problem

you cant calculate them because your fields are readonly as i think

Drees Far
on 7/24/15, 4:24 PM
1
Axel Mendoza
On 7/24/15, 8:29 PM

You just need to reorder your calculation of the fields to not override the old values. This should work:


    @api.multi
def _computeVar(self):
for record in self:
record.val_diff= record.val - record.val_aux
            record.val_aux= record.val
val=fields.Float(string='val')
val_aux=fields.Float(string='val aux',compute='_computediff', readonly=True)
val_diff= fields.Float(string='val diff',compute='_computediff', readonly=True)


1

The above code normally must give zero value in the difference field 'val_diff'

to achieve your request

I have to set a field that compute the difference between the old and the new value of a float field

I suggest the following solution instead of the one defined above,

@api.one
@api.depends('val','old_val') 
def _computediff(self):
    self.val_diff= self.val-self.old_val

val=fields.Float(string='val') 
old_val=fields.Float(string='Old Value') 
val_diff= fields.Float(string='val diff',compute='_computediff', readonly=True)

the old_val field should be updated in the write method as follows

def write(self,cr,uid,ids,vals,context=None):
    ....
    """before calling the super(<your_class_name>,self).write(cr,uid,ids,vals,context),
        gt the old_val field from the persistent database """
    o = self.pool.get('your_class_name').browse(cr,uid,ids[0])
    vals['old_val'] = o.val
    ...
    res = super(<your_class_name>,self).write(cr,uid,ids,vals,context)
    return res

you have to set the initial value of the field 'old_val' to be 0.0, this will be updated in the create method so you are sure that the _computediff will work correctly


I replaced the @api.multi with @api.one based on this link, check Computed fields and default values

 Note :I used in the openERP 7 notation for the write method

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

Question tools

2 follower(s)

Stats

Asked: 7/24/15, 12:02 PM
Seen: 5607 times
Last updated: 8/3/15, 4:52 PM