From 30271a47daefdf46a8383888f5c0f051b31864e6 Mon Sep 17 00:00:00 2001 From: Hasan Ramezani Date: Wed, 2 Oct 2019 15:19:22 +0200 Subject: [PATCH] [3.0.x] Refs #27914 -- Fixed serialization of nested enum.Enum classes in migrations. Backport of 6452112640081ac8838147a8ba192c45879203d8 from master --- django/db/migrations/serializer.py | 2 +- tests/migrations/test_writer.py | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/django/db/migrations/serializer.py b/django/db/migrations/serializer.py index 8aa6a049357..7d226955d59 100644 --- a/django/db/migrations/serializer.py +++ b/django/db/migrations/serializer.py @@ -121,7 +121,7 @@ class EnumSerializer(BaseSerializer): enum_class = self.value.__class__ module = enum_class.__module__ return ( - '%s.%s[%r]' % (module, enum_class.__name__, self.value.name), + '%s.%s[%r]' % (module, enum_class.__qualname__, self.value.name), {'import %s' % module}, ) diff --git a/tests/migrations/test_writer.py b/tests/migrations/test_writer.py index 7caca12b7d5..ca924f35ecc 100644 --- a/tests/migrations/test_writer.py +++ b/tests/migrations/test_writer.py @@ -184,6 +184,9 @@ class WriterTests(SimpleTestCase): """ Tests the migration writer (makes migration files from Migration instances) """ + class NestedEnum(enum.IntEnum): + A = 1 + B = 2 def safe_exec(self, string, value=None): d = {} @@ -289,6 +292,14 @@ class WriterTests(SimpleTestCase): IntEnum.B, ("migrations.test_writer.IntEnum['B']", {'import migrations.test_writer'}) ) + self.assertSerializedResultEqual( + self.NestedEnum.A, + ( + "migrations.test_writer.WriterTests.NestedEnum['A']", + {'import migrations.test_writer'}, + ), + ) + self.assertSerializedEqual(self.NestedEnum.A) field = models.CharField(default=TextEnum.B, choices=[(m.value, m) for m in TextEnum]) string = MigrationWriter.serialize(field)[0]