Fixed #30774 -- Made serialization in migrations use members names for Enums.
This commit is contained in:
parent
e02f67ef2d
commit
f0adf3b9b7
|
@ -120,9 +120,10 @@ class EnumSerializer(BaseSerializer):
|
||||||
def serialize(self):
|
def serialize(self):
|
||||||
enum_class = self.value.__class__
|
enum_class = self.value.__class__
|
||||||
module = enum_class.__module__
|
module = enum_class.__module__
|
||||||
v_string, v_imports = serializer_factory(self.value.value).serialize()
|
return (
|
||||||
imports = {'import %s' % module, *v_imports}
|
'%s.%s[%r]' % (module, enum_class.__name__, self.value.name),
|
||||||
return "%s.%s(%s)" % (module, enum_class.__name__, v_string), imports
|
{'import %s' % module},
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class FloatSerializer(BaseSimpleSerializer):
|
class FloatSerializer(BaseSimpleSerializer):
|
||||||
|
|
|
@ -257,6 +257,10 @@ class WriterTests(SimpleTestCase):
|
||||||
A = 'a-value'
|
A = 'a-value'
|
||||||
B = 'value-b'
|
B = 'value-b'
|
||||||
|
|
||||||
|
class TextTranslatedEnum(enum.Enum):
|
||||||
|
A = _('a-value')
|
||||||
|
B = _('value-b')
|
||||||
|
|
||||||
class BinaryEnum(enum.Enum):
|
class BinaryEnum(enum.Enum):
|
||||||
A = b'a-value'
|
A = b'a-value'
|
||||||
B = b'value-b'
|
B = b'value-b'
|
||||||
|
@ -267,15 +271,19 @@ class WriterTests(SimpleTestCase):
|
||||||
|
|
||||||
self.assertSerializedResultEqual(
|
self.assertSerializedResultEqual(
|
||||||
TextEnum.A,
|
TextEnum.A,
|
||||||
("migrations.test_writer.TextEnum('a-value')", {'import migrations.test_writer'})
|
("migrations.test_writer.TextEnum['A']", {'import migrations.test_writer'})
|
||||||
|
)
|
||||||
|
self.assertSerializedResultEqual(
|
||||||
|
TextTranslatedEnum.A,
|
||||||
|
("migrations.test_writer.TextTranslatedEnum['A']", {'import migrations.test_writer'})
|
||||||
)
|
)
|
||||||
self.assertSerializedResultEqual(
|
self.assertSerializedResultEqual(
|
||||||
BinaryEnum.A,
|
BinaryEnum.A,
|
||||||
("migrations.test_writer.BinaryEnum(b'a-value')", {'import migrations.test_writer'})
|
("migrations.test_writer.BinaryEnum['A']", {'import migrations.test_writer'})
|
||||||
)
|
)
|
||||||
self.assertSerializedResultEqual(
|
self.assertSerializedResultEqual(
|
||||||
IntEnum.B,
|
IntEnum.B,
|
||||||
("migrations.test_writer.IntEnum(2)", {'import migrations.test_writer'})
|
("migrations.test_writer.IntEnum['B']", {'import migrations.test_writer'})
|
||||||
)
|
)
|
||||||
|
|
||||||
field = models.CharField(default=TextEnum.B, choices=[(m.value, m) for m in TextEnum])
|
field = models.CharField(default=TextEnum.B, choices=[(m.value, m) for m in TextEnum])
|
||||||
|
@ -283,27 +291,39 @@ class WriterTests(SimpleTestCase):
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
string,
|
string,
|
||||||
"models.CharField(choices=["
|
"models.CharField(choices=["
|
||||||
"('a-value', migrations.test_writer.TextEnum('a-value')), "
|
"('a-value', migrations.test_writer.TextEnum['A']), "
|
||||||
"('value-b', migrations.test_writer.TextEnum('value-b'))], "
|
"('value-b', migrations.test_writer.TextEnum['B'])], "
|
||||||
"default=migrations.test_writer.TextEnum('value-b'))"
|
"default=migrations.test_writer.TextEnum['B'])"
|
||||||
|
)
|
||||||
|
field = models.CharField(
|
||||||
|
default=TextTranslatedEnum.A,
|
||||||
|
choices=[(m.value, m) for m in TextTranslatedEnum],
|
||||||
|
)
|
||||||
|
string = MigrationWriter.serialize(field)[0]
|
||||||
|
self.assertEqual(
|
||||||
|
string,
|
||||||
|
"models.CharField(choices=["
|
||||||
|
"('a-value', migrations.test_writer.TextTranslatedEnum['A']), "
|
||||||
|
"('value-b', migrations.test_writer.TextTranslatedEnum['B'])], "
|
||||||
|
"default=migrations.test_writer.TextTranslatedEnum['A'])"
|
||||||
)
|
)
|
||||||
field = models.CharField(default=BinaryEnum.B, choices=[(m.value, m) for m in BinaryEnum])
|
field = models.CharField(default=BinaryEnum.B, choices=[(m.value, m) for m in BinaryEnum])
|
||||||
string = MigrationWriter.serialize(field)[0]
|
string = MigrationWriter.serialize(field)[0]
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
string,
|
string,
|
||||||
"models.CharField(choices=["
|
"models.CharField(choices=["
|
||||||
"(b'a-value', migrations.test_writer.BinaryEnum(b'a-value')), "
|
"(b'a-value', migrations.test_writer.BinaryEnum['A']), "
|
||||||
"(b'value-b', migrations.test_writer.BinaryEnum(b'value-b'))], "
|
"(b'value-b', migrations.test_writer.BinaryEnum['B'])], "
|
||||||
"default=migrations.test_writer.BinaryEnum(b'value-b'))"
|
"default=migrations.test_writer.BinaryEnum['B'])"
|
||||||
)
|
)
|
||||||
field = models.IntegerField(default=IntEnum.A, choices=[(m.value, m) for m in IntEnum])
|
field = models.IntegerField(default=IntEnum.A, choices=[(m.value, m) for m in IntEnum])
|
||||||
string = MigrationWriter.serialize(field)[0]
|
string = MigrationWriter.serialize(field)[0]
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
string,
|
string,
|
||||||
"models.IntegerField(choices=["
|
"models.IntegerField(choices=["
|
||||||
"(1, migrations.test_writer.IntEnum(1)), "
|
"(1, migrations.test_writer.IntEnum['A']), "
|
||||||
"(2, migrations.test_writer.IntEnum(2))], "
|
"(2, migrations.test_writer.IntEnum['B'])], "
|
||||||
"default=migrations.test_writer.IntEnum(1))"
|
"default=migrations.test_writer.IntEnum['A'])"
|
||||||
)
|
)
|
||||||
|
|
||||||
def test_serialize_choices(self):
|
def test_serialize_choices(self):
|
||||||
|
@ -454,7 +474,7 @@ class WriterTests(SimpleTestCase):
|
||||||
# Test a string regex with flag
|
# Test a string regex with flag
|
||||||
validator = RegexValidator(r'^[0-9]+$', flags=re.S)
|
validator = RegexValidator(r'^[0-9]+$', flags=re.S)
|
||||||
string = MigrationWriter.serialize(validator)[0]
|
string = MigrationWriter.serialize(validator)[0]
|
||||||
self.assertEqual(string, "django.core.validators.RegexValidator('^[0-9]+$', flags=re.RegexFlag(16))")
|
self.assertEqual(string, "django.core.validators.RegexValidator('^[0-9]+$', flags=re.RegexFlag['DOTALL'])")
|
||||||
self.serialize_round_trip(validator)
|
self.serialize_round_trip(validator)
|
||||||
|
|
||||||
# Test message and code
|
# Test message and code
|
||||||
|
|
Loading…
Reference in New Issue