python - Correctly saved timezone aware datetime object appearing timezone unaware when accessed in Django app -
in django app w/ postgresql backend maintain, users log in , post comments. every comment has timestamp associated it.
when creating comment object, do:
now = datetime.now() comment.objects.create(commenter=self.request.user, when=now, text='text')
now
timezone aware - i.e. utc +530, , now
encodes information correctly. if use psql
, peek @ entry created in comment postgresql table, see correct value datetime object.
however, if access same object django app, , print
out datetime stamp, see timezone unaware object. i.e. datetime object doesn't know i'm @ utc +530.
this totally messing logic. how ensure same datetime object in django app that's saved in related postgresql table? it's remarkable 2 outputs different, i'm doing fundamentally wrong. help!
use timezone.now() instead of datetime.now():
from django.utils import timezone timezone.now()
to now() either or without timezone globally whole app (including postgresql schema updates). or without time zone depends on use_tz setting.