Skip to Content
Menu
This question has been flagged
2 Replies
3578 Views

"AttributeError: 'bool' object has no attribute 'read'" like this it refers my code line "File "/home/agile/workspace/odoo-10.0/addons/colbrow_medics/causality.py", line 41, in onchange_getage_id birth_date = parser.parse(self.dob)"

@api.onchange('dob')

def onchange_getage_id(self):

        current_date = datetime.now()

        current_year = current_date.year

        birth_date = parser.parse(self.dob)

        current_age = current_year - birth_date.year

        self.age = current_age


Why i am getting this error

Avatar
Discard
Best Answer

This example is with the validation. That validate the age must greater then 18 and less then 120.

You can set it your own needs or also can remove.

dob must be declare as a Date() field.

dob = fields.Date(string='Date of Birth')
age = fields.Integer()

@api.onchange('dob')
def onchange_getage_id(self):
     for rec in self:
         if rec.dob:
             if (parser.parse(rec.dob).year > datetime.now().year - 120 and parser.parse(rec.dob).year < datetime.now().year - 18):
                 rec.age = (datetime.now().year - parser.parse(rec.dob).year)
Avatar
Discard
Best Answer

Hi,

here parser.parse need an date object args or objects, not a bool value !

This is teh syntax:-

dateutil.parser.parse(timestr, parserinfo=None, **kwargs)

where

Parameters:   
    timestr – A string containing a date/time stamp.
    parserinfo – A parserinfo object containing parameters for the parser. If None, the default arguments to the      parserinfo constructor are used.

Avatar
Discard