diff --git a/django/db/migrations/writer.py b/django/db/migrations/writer.py index 11ecb6ed2b..b45ec0ceda 100644 --- a/django/db/migrations/writer.py +++ b/django/db/migrations/writer.py @@ -472,6 +472,9 @@ class MigrationWriter(object): "For more information, see " "https://docs.djangoproject.com/en/%s/topics/migrations/#serializing-values" % (value.__name__, module_name, get_docs_version())) + # Needed on Python 2 only + if module_name == '__builtin__': + return value.__name__, set() return "%s.%s" % (module_name, value.__name__), {"import %s" % module_name} # Other iterables elif isinstance(value, collections.Iterable): diff --git a/tests/migrations/test_writer.py b/tests/migrations/test_writer.py index ef9489748d..16d2f2f062 100644 --- a/tests/migrations/test_writer.py +++ b/tests/migrations/test_writer.py @@ -359,6 +359,11 @@ class WriterTests(SimpleTestCase): self.assertSerializedEqual(one_item_tuple) self.assertSerializedEqual(many_items_tuple) + def test_serialize_builtins(self): + string, imports = MigrationWriter.serialize(range) + self.assertEqual(string, 'range') + self.assertEqual(imports, set()) + @unittest.skipUnless(six.PY2, "Only applies on Python 2") def test_serialize_direct_function_reference(self): """