Reputation: 1357
I am trying to add a DateTimeField to my django model with the maximum timestamp as default value. I already figured out, that the maximum timestamp in Django is 9999/12/31 23:59:59 unlike the maximum timestamp used in my postgres DB. When using this timestamp as default value for the field I get an OverflowError: date value out of range
error. Therefore I tried the same with 9999/01/01 00:00:00 like so:
start_time = models.DateTimeField(null=False, default=datetime.datetime(9999,01,01,00,00,00,tzinfo=utc))
Now, when I apply this south migration to the database, I get the following exception:
RuntimeWarning: DateTimeField received a naive datetime (9999-01-01 00:00:00) while time zone support is active.
When looking into the database, I see that my local timezone has been applied to the default value: 9999-01-01 00:00:00+01
This leads me to the conclusion, that somehow the timezone awareness is ignored by django, but I have no idea why that could be.
Additional Info:
Django TIME_ZONE is set to 'Europe/Berlin'
USE_TZ is True
Any help is appreciated.
Upvotes: 4
Views: 6405
Reputation: 3368
Try calling replace
on the datetime instead of passing the tzinfo into the __init__
of the datetime. Taken from the django docs:
import datetime
from django.utils.timezone import utc
now = datetime.datetime(9999, 1, 1).replace(tzinfo=utc)
Upvotes: 6