From 09188b50247fe3efa1d22cd3f9971bf4589e7cbe Mon Sep 17 00:00:00 2001 From: Baptiste Mispelon Date: Thu, 2 Apr 2015 16:49:41 +0200 Subject: [PATCH] [1.8.x] Fixed #24566 -- Added support for serializing timedelta Thanks to knbk for the report. Backport of 30a3c2f74c030449d3a8b9f02ab6744874712e54 from master. --- django/db/migrations/writer.py | 3 +++ docs/releases/1.8.1.txt | 3 ++- tests/migrations/test_writer.py | 5 +++++ 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/django/db/migrations/writer.py b/django/db/migrations/writer.py index 999f5c86469..c91ee6870f3 100644 --- a/django/db/migrations/writer.py +++ b/django/db/migrations/writer.py @@ -323,6 +323,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 c449eab27e2..1dff6170636 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 79a374b5673..4573bda3f0b 100644 --- a/tests/migrations/test_writer.py +++ b/tests/migrations/test_writer.py @@ -339,6 +339,11 @@ class WriterTests(TestCase): self.assertSerializedEqual(FoodManager('a', 'b')) 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): """ Tests serializing a simple migration.