[Answered ]-Django datetime field prints out unsupported operand type error

1👍

The traceback error is explaining the problem to you:

unsupported operand type(s) for -: ‘datetime.datetime’ and ‘datetime.date’

This says that you cannot subtract a datetime.datetime from a datetime.date. In your case, self.created_at appears to be a date but your use of datetime.now() and timedelta() suggest that you really want to be dealing with datetimes.

Therefore, the solution is to either

  1. make sure that created_at is a datetime, or
  2. change datetime.now() to date.today() and change timedelta(hours=48) to timedelta(days=2)

These will behave a little differently so pick one depending on whether things expire after two calendar days or in 48 hours.

Leave a comment