From 29d8198841ea39af44f3bc835d646e642d498475 Mon Sep 17 00:00:00 2001 From: John Bowen Date: Fri, 22 Nov 2019 12:43:03 +0000 Subject: [PATCH] Fixed #27914 -- Fixed serialization of nested classes in migrations. --- django/db/migrations/serializer.py | 2 +- tests/migrations/test_writer.py | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/django/db/migrations/serializer.py b/django/db/migrations/serializer.py index 7d226955d59..ead81c398a6 100644 --- a/django/db/migrations/serializer.py +++ b/django/db/migrations/serializer.py @@ -269,7 +269,7 @@ class TypeSerializer(BaseSerializer): if module == builtins.__name__: return self.value.__name__, set() else: - return "%s.%s" % (module, self.value.__name__), {"import %s" % module} + return "%s.%s" % (module, self.value.__qualname__), {"import %s" % module} class UUIDSerializer(BaseSerializer): diff --git a/tests/migrations/test_writer.py b/tests/migrations/test_writer.py index addf10b0f03..6a65e87d5a3 100644 --- a/tests/migrations/test_writer.py +++ b/tests/migrations/test_writer.py @@ -193,6 +193,10 @@ class WriterTests(SimpleTestCase): A = 1 B = 2 + class NestedChoices(models.TextChoices): + X = 'X', 'X value' + Y = 'Y', 'Y value' + def safe_exec(self, string, value=None): d = {} try: @@ -388,6 +392,18 @@ class WriterTests(SimpleTestCase): "default=datetime.date(1969, 11, 19))" ) + def test_serialize_nested_class(self): + for nested_cls in [self.NestedEnum, self.NestedChoices]: + cls_name = nested_cls.__name__ + with self.subTest(cls_name): + self.assertSerializedResultEqual( + nested_cls, + ( + "migrations.test_writer.WriterTests.%s" % cls_name, + {'import migrations.test_writer'}, + ), + ) + def test_serialize_uuid(self): self.assertSerializedEqual(uuid.uuid1()) self.assertSerializedEqual(uuid.uuid4())