我想让代码可以计算年龄
def _compute_age(self, cr, uid, ids, field_name, field_value, context=None):
records = self.browse(cr, uid, ids, context=context)
result={}
for r in records:
age=0
if r.date_birth:
d = strptime(r.date_birth,"%Y-%m-%d")
count = date(d[0],d[1],d[2])-date.today()
age = count.days/365
result[r.id] = age
return result
但是错误的“光标”对象没有“浏览”属性,有什么问题?
PS:这是我的新代码
def _compute_age(self, cr, uid, ids,field_name,field_value,arg, context=None):
records = self.browse(cr, uid, ids, context=context)
result = {}
for r in self.browse(cr, uid, ids, context=context):
age=0
if r.date_birth:
print date_birth
age = (datetime.now()-datetime.strptime(r.date_birth,"%Y-%m-%d")).days/365.25
result[r.id] = age
return result
_columns = {
'date_birth': fields.date('Date of Birth'),
'age' : fields.function(_compute_age, type='char', method=True, string='Age'),