diff --git a/django/db/migrations/writer.py b/django/db/migrations/writer.py index debbadeded..1b9cdb886e 100644 --- a/django/db/migrations/writer.py +++ b/django/db/migrations/writer.py @@ -212,7 +212,12 @@ class MigrationWriter(object): strings.append((k_string, v_string)) return "{%s}" % (", ".join("%s: %s" % (k, v) for k, v in strings)), imports # Datetimes - elif isinstance(value, (datetime.datetime, datetime.date)): + elif isinstance(value, datetime.datetime): + if value.tzinfo is not None: + raise ValueError("Cannot serialize datetime values with timezones. Either use a callable value for default or remove the timezone.") + return repr(value), set(["import datetime"]) + # Dates + elif isinstance(value, datetime.date): return repr(value), set(["import datetime"]) # Settings references elif isinstance(value, SettingsReference): diff --git a/tests/migrations/test_writer.py b/tests/migrations/test_writer.py index 14dee65a51..8bdce5d8c0 100644 --- a/tests/migrations/test_writer.py +++ b/tests/migrations/test_writer.py @@ -13,6 +13,7 @@ from django.conf import settings from django.utils import six from django.utils.deconstruct import deconstructible from django.utils.translation import ugettext_lazy as _ +from django.utils.timezone import get_default_timezone class WriterTests(TestCase): @@ -78,6 +79,8 @@ class WriterTests(TestCase): self.assertSerializedEqual(datetime.datetime.today) self.assertSerializedEqual(datetime.date.today()) self.assertSerializedEqual(datetime.date.today) + with self.assertRaises(ValueError): + self.assertSerializedEqual(datetime.datetime(2012, 1, 1, 1, 1, tzinfo=get_default_timezone())) # Classes validator = RegexValidator(message="hello") string, imports = MigrationWriter.serialize(validator)