简体   繁体   中英

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

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

eg

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM