Other solution is to use a functional field to compute the age at this moment. Following is this approach, with an additional method to search people from his age:
def _years_old_get(self, cr, uid, ids, field_name, arg, context={}): result = {} for rec in self.browse(cr, uid, ids, context): result[rec.id] = False if rec.birth_date: birth = time.strptime(rec.birth_date, '%Y-%m-%d') #print rec.birth_date, birth try: result[rec.id] = (time.time() - time.mktime(birth)) / (365.25 * 86400) # 365.25 days of 86400 seconds except OverflowError: result[rec.id] = 100 # If it is more than 100 years old return result def _years_old_search(self, cr, uid, obj, name, args): def year_date(param): oper = param[1] if oper[:1] == '<': oper = '>'+oper[1:] elif oper[:1] == '>': oper = '<'+oper[1:] year = param[2] dbirth = time.time() - year * 365.25 * 86400 # Don't accept dates older than 1901-12-14 (aprox.) dbirth = dbirth>-2147400000 and dbirth or -2147400000 dbirth = time.gmtime(dbirth) date_birth = "%04i-%02i-%02i" % (dbirth.tm_year, dbirth.tm_mon, dbirth.tm_mday) #print dbirth, date_birth return "(birth_date "+oper+" '"+date_birth+"')" if not len(args): return [] where = ' and '.join(map(year_date, args)) #print args, where cr.execute('select id from res_partner_address l where ' + where) res = cr.fetchall() if not len(res): return [('id','=','0')] return [('id','in',map(lambda x:x[0], res))] _columns = { 'birth_date': fields.date('Birth date), 'years_old': fields.function(_years_old_get, method=True, type="float", string="Age", fnct_search=_years_old_search), } ------------------------ Jordi Esteve http://www.zikzakmedia.com/es/openerp.html Zikzakmedia SL -------------------- m2f -------------------- -- http://www.openobject.com/forum/viewtopic.php?p=51161#51161 -------------------- m2f -------------------- _______________________________________________ Tinyerp-users mailing list http://tiny.be/mailman2/listinfo/tinyerp-users
