Skip to Content
Menu
This question has been flagged
3 Replies
2512 Views

Hi 

I have this problem when i wanna create new employe 

in _compute_seniority TypeError: must be string, not bool

my function is 

@api.depends('date_rejoindre')
def _compute_seniority(self):
for rec in self:
dt = rec.date_rejoindre
d2 = date.today()
d1 = datetime.strptime(dt, "%Y-%m-%d").date()   # the line of the error
rd = relativedelta(d2, d1)
rec.anciennete = str(rd.years) + ' Ans ' + str(rd.months) + ' Mois ' + str(rd.days) + ' Jours'

THANK TOU





Avatar
Discard
Best Answer

Hello, 

you can add IF condition maybe in your case return value False. So add one condition in your code if condition. 
In this case, you can update your code according to describe below maybe you get the solution.

@api.depends('date_rejoindre') 
def _compute_seniority(self): 
for rec in self: 
dt = rec.date_rejoindre 
d2 = date.today() 
if dt and d2:
    d1 = datetime.strptime(dt, "%Y-%m-%d").date()   # the line of the error
    rd = relativedelta(d2, d1) 
    rec.anciennete = str(rd.years) + ' Ans ' + str(rd.months) + ' Mois ' + str(rd.days) + ' Jours'

Thanks 

Avatar
Discard
Best Answer

Hi,

This error occurs when that field is not date string or it  contains null value, so it returns False value when it is called.So first make sure that field contains date str when function is called strptime method requires date string value.

You can use if condition to check whether data is present in field continue strptime method or if you're using pycharm you can add breakpoint to check values

Similar question check this link


Avatar
Discard
Best Answer

what version of odoo is it?

if it's 12, the date is already a date, you don't need to convert it

just comment the line d1 = datetime.strptime(dt, "%Y-%m-%d").date() 



Avatar
Discard