Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed #21954: Raise nice error when serializing datetimes with timezones

  • Loading branch information...
commit 97a8fd4682b27a3a9f280e661e58db4bae55590a 1 parent 4f8e8a6
@andrewgodwin andrewgodwin authored
View
7 django/db/migrations/writer.py
@@ -212,7 +212,12 @@ def serialize(cls, value):
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):
View
3  tests/migrations/test_writer.py
@@ -13,6 +13,7 @@
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 @@ def test_serialize(self):
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)
Please sign in to comment.
Something went wrong with that request. Please try again.