Search code examples
pythondjangodjango-rest-frameworkdjango-timezone

Django / DRF - Changing the timezone does not work


This is my settings.py:

TIME_ZONE = 'America/Toronto'

USE_I18N = True

USE_L10N = True

USE_TZ = True

Before, it used to be TIME_ZONE = 'UTC' but I just changed it right now. I restarted the development server after changing it and created a post but the date and time of when the post was created is still now according to the Toronto timezone (it still follows the previous timezone it was in). My model is using the default DateTimeField:

createdAt = models.DateTimeField(auto_now_add=True, blank=True)

Is there anything else which needs to be changed in order for the timezone changes to take effect? I just tried doing

python manage.py makemigrations
python manage.py migrate

but it said no changes detected (as expected).

Edit: Note that I am also using DRF and serializers to serialize the posts. I'm not sure if that makes a difference though (do I need to change any DRF settings?).


Solution

  • When support for time zones is enabled, Django stores datetime information in UTC in the database, uses time-zone-aware datetime objects internally, and translates them to the end user’s time zone in templates and forms. django docs

    so try this

    USE_TZ = False