This question has been flagged
1 Reply
2568 Views

@api.multi
@api.onchange('exam_date')
def onchange_date_onday(self):
for rec in self:
if rec.exam_date:
week_day = datetime.strptime(rec.exam_date, "%Y-%m-%d")
rec.day_of_week = week_day.strftime("%A").title()

i wrote this code getting this error what i will do anybody can help me


Avatar
Discard
Best Answer

Hi,

You might have defined the following line outside the if statement,

rec.day_of_week = week_day.strftime("%A").title()


If the value for the rec.exam_date field is not there, the value for the variable week_day is not assigned/defined. Then outside the if statement you are accessing the same undefined the variable, thus you get this error.


You can update your code like this,

if rec.exam_date:
    week_day = datetime.strptime(rec.exam_date, "%Y-%m-%d")

    if week_day:
        rec.day_of_week = week_day.strftime("%A").title()

Thanks

Avatar
Discard
Author

thank you soomuch niyas its working