45👍
✅
Just always use timezone.now()
. Django now has timezone support which requires timezone ‘aware’ datetime objects. datetime.now()
will return a timezone naive object, whereas timezone.now()
will return a timezone aware object.
👤dgel
4👍
You can write in shell, for example:
timezone.datetime.now() < timezone.now()
And the error message is:
TypeError: can't compare offset-naive and offset-aware datetimes
They are different objects, only timezone.now() have UTC support
- [Django]-Enforce unique upload file names using django?
- [Django]-Request.data in DRF vs request.body in Django
- [Django]-ProgrammingError: relation "django_session" does not exist error after installing Psycopg2
2👍
If you want to use UTC, and you’re using Python 3.2 or higher, this answer says you can do:
import datetime
datetime.datetime.now(datetime.timezone.utc)
This will give you a timezone-aware UTC datetime.
- [Django]-Django query filter with variable column
- [Django]-ValueError: Missing staticfiles manifest entry for 'favicon.ico'
- [Django]-Celery task that runs more tasks
Source:stackexchange.com