delta24
delta24

Reputation: 173

Django error, coercing to Unicode: need string or buffer, datetime.timedelta found

Here is the code where the error occurs:

date_arrive = datetime.datetime.strptime(request.session.get('arrival'), '%m/%d/%Y'),\
date_depart = date_arrive + datetime.timedelta(days=request.session.get('nights')))).save()

And everytime Django reaches this view, it throws the error:

Exception Value: coercing to Unicode: need string or buffer, datetime.timedelta found

Here arrival is a valid datetime object and nights is an integer.

Upvotes: 0

Views: 2676

Answers (2)

Joseph Dattilo
Joseph Dattilo

Reputation: 875

You should be able to solve this problem by making sure non-unicode/non-strings like integers and datetime objects are surrounded with a str(your_date_object_here).

When I encountered this error in Django I was able to fix it with both the following:

def __str__(self): 
    return str(self.datetimeobject) + " other string return info"

and

def __str__(self):
    return unicode(self.datetimeobject) + " other string return info" 

Upvotes: 0

I S
I S

Reputation: 437

Id suggest you include full listing of function call.

Seems that you try concatenating unicode and datatime object, that is not possible. You have to convert date_arrive to datetime or "datetime.timedelta(days=request.session.get('nights')" to unicode evidently, depending on type your function needs as date_depart argument.

Upvotes: 1

Related Questions