diff --git a/django/db/migrations/writer.py b/django/db/migrations/writer.py index f4f43ac930..99db9d18ae 100644 --- a/django/db/migrations/writer.py +++ b/django/db/migrations/writer.py @@ -351,6 +351,9 @@ class MigrationWriter(object): if isinstance(value, datetime_safe.time): value_repr = "datetime.%s" % value_repr return value_repr, {"import datetime"} + # Timedeltas + elif isinstance(value, datetime.timedelta): + return repr(value), {"import datetime"} # Settings references elif isinstance(value, SettingsReference): return "settings.%s" % value.setting_name, {"from django.conf import settings"} diff --git a/docs/releases/1.8.1.txt b/docs/releases/1.8.1.txt index c449eab27e..1dff617063 100644 --- a/docs/releases/1.8.1.txt +++ b/docs/releases/1.8.1.txt @@ -9,4 +9,5 @@ Django 1.8.1 fixes several bugs in 1.8. Bugfixes ======== -* ... +* Added support for serializing :class:`~datetime.timedelta` objects in + migrations (:ticket:`24566`). diff --git a/tests/migrations/test_writer.py b/tests/migrations/test_writer.py index aa12c7c612..d2bd491635 100644 --- a/tests/migrations/test_writer.py +++ b/tests/migrations/test_writer.py @@ -351,6 +351,10 @@ class WriterTests(TestCase): self.assertSerializedEqual(frozenset()) self.assertSerializedEqual(frozenset("let it go")) + def test_serialize_timedelta(self): + self.assertSerializedEqual(datetime.timedelta()) + self.assertSerializedEqual(datetime.timedelta(minutes=42)) + def test_simple_migration(self): """ Tests serializing a simple migration.