How to make consistant timezone across Django.
In my settings.py
I have TIME_ZONE = 'America/New_York'
and USE_TZ = True
. I have a model
def MyModel(models.Model):
date_time = models.DateTimeField(auto_now_add=True)
if I view a field from that model (say pk=1) it correctly displays value for an eastern
timezone. From my terminal, when I run python manage.py shell
and make a query for that same field (pk=1), it will print the date_time attribute of that field in UTC format as follows: datetime.datetime(2020, 10, 22, 0, 19, 28, 696739, tzinfo=<UTC>)
Why is this happening? And how can I make it so that all queries use the correct timezone when filtering by date?
Django template level automatically converts the UTC to your local time. But during in model level, they are treated in UTC.
You can use the builtin timezone utils
localtime
to convert to your desired output.