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

Compute field V8

By
Helmi Dhaoui
on 7/24/15, 12:02 PM 2,012 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

--Axel Mendoza--
10318
| 7 7 8
Camaguey, Cuba
--Axel Mendoza--

DevOps - Full stack - Software Architect - Developer - Technology Integrator

I could help you to develop anything and solve complex problems based on technologies, integrations and tricky stuffs mostly in Python with OpenERP/Odoo, Zato, Django and many others frameworks programming languages and technologies.

I offers consulting services to anyone with an unanswered questions or needs for customizations. Think about it, maybe it's better to have an expert to solve your issues and projects than having a full time employee trying to understand what to do an how

Reach me at aekroft@gmail.com

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

Tarek Mohamed Ibrahim

--Tarek Mohamed Ibrahim--
896
| 5 3 7
Gîza, Egypt
--Tarek Mohamed Ibrahim--

I am an old VFP developer on ERP

I have moved to 2p since Nov-2014 and started developing with Python on Odoo.

https://www.linkedin.com/profile/public-profile-settings?trk=prof-edit-edit-public_profile

tibrahim@2p.com.sa

Tarek Mohamed Ibrahim
On 7/24/15, 8:38 PM

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

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: 7/24/15, 12:02 PM
Seen: 2012 times
Last updated: 8/3/15, 4:52 PM