0
votes

I want to calculate the value of a function field using its previous value ( = value of the record with previous id)

'testrest' : fields.function(get_reste, method=True, string='Restant',type='integer'),

 def get_reste(self, cr, uid, ids, field_name, arg, context):
    x = {}
    for record in self.browse(cr, uid, ids ,context):
            if record.statut !=  'entree': 
                x[record.id]= a + record.entree_nbr # a should be the same field for the previous record

How can I do that? thank you

2

2 Answers

1
votes

First point here about OE 6.1+ and fields.function() : it does not take a method parameter anymore [ Server rev 3495 revid [email protected] and addons rev 4844].So please do not use the "method" parameter anymore!

Now you want to calculate the value based on previous value so what you can do is you can use store=True param here that will store your previous value in data now in your calculation for your record you can read previous value and calculate new value and return it.

'testrest' : fields.function(get_reste, store=True, string='Restant',type='integer'),

 def get_reste(self, cr, uid, ids, field_name, arg, context):
    x = {}
    for record in self.browse(cr, uid, ids ,context):
            if record.statut !=  'entree': 
                x[record.id]= record.testrest + record.entree_nbr
    return x

Here benefit of string will be you can use this value any where out side OE or fro some external reporting tool and you can even expoer this field.

Hope this will help.

Some More Code :

'price': fields.function(_price_get, method=True, string="Price", store=True),
1
votes
def get_reste(self, cr, uid, ids, field_name, arg, context):
    x = {}
    a = 0.0
    for record in self.browse(cr, uid, ids ,context):
            if record.statut !=  'entree': 
                x[record.id]= a + record.entree_nbr 
                a =record.testrest
    return x

If you need you can sort the list of ids by ids.sort()