Odoo Help


function field

on 3/11/13, 4:38 PM 975 views

I have an object with field state (draft and done), and field compteur, i want to increment compteur until the the field change the state to done,

The problem is when i valid the state, the field compteur become zero, and i want to stay in last value

this is my code :

def my_function(self, cr, uid, ids, field_name, arg, context):
    res = {}
    for cons in self.browse(cr, uid, ids, context=context):
        if cons.state == 'draft' :
                compteur_mtn = 0
        date_creation_repertoire= cons.date_creation_repertoire
        date_mtn= time.strftime('%Y-%m-%d')
        date_creation_repertoire = date_creation_repertoire.split('-')
        date_mtn= date_mtn.split('-')
        jours1 = 0
        jours2 = 0
        jours1 = ((int(date_mtn[0]) * 365) + (int(date_mtn[1]) * 30) + int((date_mtn[2])))
        print jours1
        jours2 = ((int(date_creation_repertoire[0]) * 365) + (int(date_creation_repertoire[1]) * 30) + int((date_creation_repertoire[2])))
        print jours2
        compteur = (jours1 - jours2)
        print compteur
        res[cons.id] = compteur
        else : 
        res[cons.id] = False
    return res

    'compteur': fields.function(my_function, type="float", string='Délai en j'),

Andreas Brueckl

--Andreas Brueckl--
Andreas Brueckl
| 7 8 7
Vienna, Austria
--Andreas Brueckl--
OpenERP Consulting and Development
Andreas Brueckl
On 3/12/13, 4:03 PM

I would use a 2nd field:

'compteur_store': fields.float("not used"),

Then I would rewrite the function:

def my_function(self, cr, uid, ids, field_name, arg, context):
    res = {}
    for cons in self.browse(cr, uid, ids, context=context):
        if cons.compteur_store:
            res[cons.id] = cons.compteur_store
            original function ....

Additonally you have to write the field compteur_store when the state is set to done:

def write(self, cr, uid, ids, vals, context=None):
    if vals.get('state', '') == 'done':
        vals['compteur_store'] = self.read(cr, uid, ids[0], ['compteur'])[0]['compteur']
    res = super(my_class, self).write(cr, uid, ids, vals, context=context)
    return res

The write() function is only an example. You should use a loop over all ids. But I hope that the suggested principle is clear.

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 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.


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

0 follower(s)


Asked: 3/11/13, 4:38 PM
Seen: 975 times
Last updated: 3/16/15, 8:10 AM