Odoo Help


How to do a field like Age of the client? [Closed]

Anabela Damas
on 11/5/13, 7:38 AM 1,694 views

The Question has been closed

Anabela Damas
on 12/06/2013 11:25:37


My res_partner need a field with the Age of client, I've tried two different ways with onchange and a field function.

First way - onchange:

py file:

 class res_partner(osv.osv):
        _name = 'res.partner'
        _inherit = 'res.partner'
        def calc_age(self, cr, uid, ids, date_):
            values = {}
            age = 0

            date_actual = datetime.strptime(time.strftime("%Y-%m-%d"), "%Y-%m-%d")
            date_ = datetime.strptime(date_,"%Y-%m-%d")

            if date_:    
                delta = date_actual - date_
                if date_actual.month < date_.month or \
                  (date_actual.month == date_.month and date_actual.day < date_.day):
                    delta = date_actual.year - date_.year - 1
                    delta = date_actual.year - date_.year
            values = {'age': delta}
            return {'value' : values}

    _columns = {
    'age': fields.integer('Age'),

view file:

<field name="date_of_birth" on_change="calc_age(date_of_birth)"  context="{'default_age': age}"/>  
<field name="age"/>

This way if I change the birth date the age is immediately updated, but it isn't updated with the pass of the years! So I tried the second way - fields.funtion:

in py file :

 class res_partner(osv.osv):
        _name = 'res.partner'
        _inherit = 'res.partner'
def _compute_age(self, cr, uid, ids, field_name, arg, context={}):
    result = {}
    now = datetime.now()
    for r in self.browse(cr, uid, ids, context=context):
      if r.data_nascimento:
        dob = datetime.strptime(r.data_nascimento,'%Y-%m-%d')
        delta=relativedelta (now, dob)
        result[r.id] = str(delta.years) 
        result[r.id] = "No DoB !"
    return result

    _columns = {
    'age' : fields.function(_compute_age, method=True, type='char', size=32, string='Age',),

In view file:

     <field name="date_of_birth" />   
<field name="age"/>

With the fields.function I change the birth date and the Age isn't updated. The Age is only computed when I click in the save button that the Age is compute.

It is a way to update date when I change the birth date and to update with the pass of the years?


Daniel Reis

--Daniel Reis--
| 7 8 9
Linda-a-Velha, Portugal
--Daniel Reis--

Author of the "Odoo Development Essentials" book.

Applications Manager at Securitas Portugal

Github: https://github.com/dreispt

Twitter; @reis_pt

Daniel Reis
On 11/5/13, 5:22 PM

Function fields are the right solution for the problem, but they are only evaluated after saving.

In case you really need the valu calculated before saving (for example, to use in attrs on other form fields), you need to also use the in_change method. It's a kind of a hack, but it's the only solution.

Hopefully the new server API for v8 will improve the on_change handling to avoid this problem.

PS: I'm wondering, is it really important to calculate the age before saving. As far as I can see the function field approach alone seems to be good enough.

Thanks, my client ask... so it's important. =)

Versão Integral, Anabela Damas
on 11/6/13, 6:31 AM

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: 11/5/13, 7:38 AM
Seen: 1694 times
Last updated: 3/16/15, 8:10 AM