[1.8.x] Fixed #24566 -- Added support for serializing timedelta

Thanks to knbk for the report.

Backport of 30a3c2f74c from master.
This commit is contained in:
Baptiste Mispelon 2015-04-02 16:49:41 +02:00
parent e7366ab1f9
commit 09188b5024
3 changed files with 10 additions and 1 deletions

View File

@ -323,6 +323,9 @@ class MigrationWriter(object):
if isinstance(value, datetime_safe.time): if isinstance(value, datetime_safe.time):
value_repr = "datetime.%s" % value_repr value_repr = "datetime.%s" % value_repr
return value_repr, {"import datetime"} return value_repr, {"import datetime"}
# Timedeltas
elif isinstance(value, datetime.timedelta):
return repr(value), {"import datetime"}
# Settings references # Settings references
elif isinstance(value, SettingsReference): elif isinstance(value, SettingsReference):
return "settings.%s" % value.setting_name, {"from django.conf import settings"} return "settings.%s" % value.setting_name, {"from django.conf import settings"}

View File

@ -9,4 +9,5 @@ Django 1.8.1 fixes several bugs in 1.8.
Bugfixes Bugfixes
======== ========
* ... * Added support for serializing :class:`~datetime.timedelta` objects in
migrations (:ticket:`24566`).

View File

@ -339,6 +339,11 @@ class WriterTests(TestCase):
self.assertSerializedEqual(FoodManager('a', 'b')) self.assertSerializedEqual(FoodManager('a', 'b'))
self.assertSerializedEqual(FoodManager('x', 'y', c=3, d=4)) self.assertSerializedEqual(FoodManager('x', 'y', c=3, d=4))
def test_serialize_timedelta(self):
self.assertSerializedEqual(datetime.timedelta())
self.assertSerializedEqual(datetime.timedelta(minutes=42))
def test_simple_migration(self): def test_simple_migration(self):
""" """
Tests serializing a simple migration. Tests serializing a simple migration.