6👍
✅
Subtracting two datetime
objects in Python gives you a timedelta
object, which you can’t compare to an integer. You can, however, get something like total_seconds()
from it to see if it’s nonzero.
>>> now = datetime.datetime.now()
# wait 3 seconds
>>> now2 = datetime.datetime.now()
>>> td = now2 - now
>>> td.total_seconds()
3.266
So in your case, the code would look something like (untested):
def is_due_today(self):
dd = self.due_date
delta = dd - datetime.date.today()
return delta.total_seconds() > 0
2👍
today
should be:
today = datetime.date.today()
Subtracting two datetime date objects gives you a timedelta.
>>> a - datetime.date.today()
datetime.timedelta(0)
>>> a = datetime.date.today()
>>> a - a
datetime.timedelta(0)
>>> a - a == 0
False
>>>
>>> a - a == datetime.timedelta(0)
True
Also, if you are checking if something is due today, your conditional statement might need to be changed:
today - today isn't going to be > 0
If you are checking for past due:
return self.due_date < datetime.date.today()
Checking for due today:
return self.due_date == datetime.date.today()
- [Django]-How to pass args to a signal
- [Django]-Crispy Forms Initial data
- [Django]-Django Meta ordering in related query
- [Django]-Django method to change User email not working
Source:stackexchange.com