mirror of https://github.com/django/django.git
Fixed #21954: Raise nice error when serializing datetimes with timezones
This commit is contained in:
parent
4f8e8a6ec2
commit
97a8fd4682
|
@ -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):
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in New Issue