Tags

, ,


.py file

'age': fields.char('Age',size=64),

.xml file

<field name="dob" on_change="get_age(dob)"/>

python function

odoo 8
from datetime import date,datetime
def get_age(self, cursor, user, ids, dob, context=None):
    res={}
    if dob:
        dob = datetime.strptime(dob, "%Y-%m-%d").date()
        today = date.today()
        age_cal = relativedelta(today,dob)
        age= str(age_cal.years) + ' Years'
        res =  {'age': age}
    return {'value': res}
odoo 10 import time 
from datetime import datetime
age = fields.Char(string="Age", required=False, )

@api.onchange('dob')
def get_age(self):
 res = {}
 if self.dob:
 dob = datetime.strptime(self.dob, "%Y-%m-%d")
 age_calc = (datetime.today() - dob).days/365
 age= str(age_calc) + ' Years'
 self.age = age

 

 

 

 

 

 

Advertisements