Himanshu
Himanshu

Reputation: 65

Comparing datetime field in python, not just date or time , Type Error :Can't compare unicode to date.datetime field

Have created a Django datetime field 'time' and want to have some comparisions based on that. But it gives the error that Can't compare unicode to date.datetime field

here's what I am doing

if form.is_valid():
   if formdata['time']==datetime.datetime.min:
       formdata['time'] = datetime.date.now
   if formadata['time'] < last_entry_time:
       error_message

What I could figure out was, that the first condition returns a false even if they are equal ( by default set to min.) and in the second if condition it gives the error..

Wen through the python documentation for datetime field and its mentioned that it doesn't raise a type error for '==' and '!=' comparison. So basically there is type mismatch in both the conditional statements

Upvotes: 0

Views: 945

Answers (1)

Rohan
Rohan

Reputation: 53336

Use attributes in form.cleaned_data to compare. Attributes in cleaned_data will be of appropriate data type.

e.g.

if form.is_valid():
   if form.cleaned_data['time']==datetime.datetime.min:
   ....

Upvotes: 1

Related Questions