[Django]-Freezegun always causes RuntimeWarning of receiving naive datetime

3👍

Seems like you’re trying to save an object with a timezone-naive datetime. To get rid of this warning, just use timezone-aware datetime everywhere in your application.


Instead of managing timezone yourself manually using pytz, you can use Django’s timezone module found at django.utils.timezone . It has some shortcut methods that you can use to convert naive datetime to aware datetime.

An advantage of using this is that if you ever change the timezone settings in your settings file, it will automatically pick the new timezone, whereas with pytz you’ll have to manually update the new timezone everywhere.

from django.utils import timezone

fake_datetime = timezone.make_aware(timezone.datetime(2000, 1, 1, 0, 0, 0))
👤xyres

Leave a comment