diff --git a/django/db/migrations/writer.py b/django/db/migrations/writer.py index ac5da0953b..82396c1033 100644 --- a/django/db/migrations/writer.py +++ b/django/db/migrations/writer.py @@ -351,7 +351,10 @@ class MigrationWriter(object): return string, set(imports) if hasattr(value, "__module__"): module = value.__module__ - return "%s.%s" % (module, value.__name__), set(["import %s" % module]) + if module == six.moves.builtins.__name__: + return value.__name__, set() + else: + return "%s.%s" % (module, value.__name__), set(["import %s" % module]) # Other iterables elif isinstance(value, collections.Iterable): imports = set() diff --git a/docs/releases/1.7.1.txt b/docs/releases/1.7.1.txt index a1149d37cd..2c8af6ac7e 100644 --- a/docs/releases/1.7.1.txt +++ b/docs/releases/1.7.1.txt @@ -83,3 +83,6 @@ Bugfixes * Coerced the ``related_name`` model field option to unicode during migration generation to generate migrations that work with both Python 2 and 3 (:ticket:`23455`). + +* Fixed ``MigrationWriter`` to handle builtin types without imports + (:ticket:`23560`). diff --git a/tests/migrations/test_writer.py b/tests/migrations/test_writer.py index 9a04df0553..2b7ceddb2d 100644 --- a/tests/migrations/test_writer.py +++ b/tests/migrations/test_writer.py @@ -81,6 +81,11 @@ class WriterTests(TestCase): self.assertSerializedEqual(set([2, 3, "eighty"])) self.assertSerializedEqual({"lalalala": ["yeah", "no", "maybe"]}) self.assertSerializedEqual(_('Hello')) + # Builtins + self.assertSerializedEqual([list, tuple, dict, set]) + string, imports = MigrationWriter.serialize([list, tuple, dict, set]) + self.assertEqual(string, "[list, tuple, dict, set]") + self.assertEqual(imports, set()) # Functions with six.assertRaisesRegex(self, ValueError, 'Cannot serialize function: lambda'): self.assertSerializedEqual(lambda x: 42)