Django migrations and DateTimeField defaults

1,179 views
Skip to first unread message

Andy Roxby

unread,
Aug 12, 2014, 12:15:18 PM8/12/14
to django...@googlegroups.com
Hello all,
I'm new to this list so please let me know I'm overlooking any sort of etiquette.
I'm having problem trying to use a default value for a DateTimeField.  When I specify a default value WITH a timezone I receive the following error during './manage.py makemigrations'

ValueError: Cannot serialize datetime values with timezones. Either use a callable value for default or remove the timezone.

However, if I remove the timezone I receive the following warning during 'migrate'

RuntimeWarning: DateTimeField Verification_link.redeemed received a naive datetime (9999-12-31 23:59:59.999999) while time zone support is active.

I'm just a little confused, I'm crafting a work-around but I would like to know what the correct solution is.  Any help would be appreciated.

Code:
from datetime import datetime
from django.utils import timezone

INF_TIME=datetime.max.replace(tzinfo=timezone.utc)

class SomeModel: death = models.DateTimeField(default=INF_TIME)

Collin Anderson

unread,
Aug 12, 2014, 9:43:11 PM8/12/14
to django...@googlegroups.com
Wow. That almost sounds like a bug, though you can however use a callable. As ridiculous as it looks, it should work :).

INF_TIME = datetime.max.replace(tzinfo=timezone.utc)

def get_inf_time():
   
return INF_TIME

death
= models.DateTimeField(default=get_inf_time)

What you are doing seems pretty rare, though it seems maybe that error message should not be recommending a naive datetime.

Andy Roxby

unread,
Aug 13, 2014, 11:01:36 AM8/13/14
to django...@googlegroups.com
Thanks, that's what I did.  In my humble opinion, the ValueError should be removed and the timezone serialized in the form UTC offset (eg: -0400).
Reply all
Reply to author
Forward
0 new messages