Fixed #33916 -- Added support for serialization of enum.Flag in migrations.

This commit is contained in:
Willem Van Onsem 2022-08-11 18:17:19 +02:00 committed by Mariusz Felisiak
parent b30c0081d4
commit 16fffc829c
4 changed files with 29 additions and 4 deletions

View File

@ -16,7 +16,7 @@ from django.db import models
from django.db.migrations.operations.base import Operation
from django.db.migrations.utils import COMPILED_REGEX_TYPE, RegexObject
from django.utils.functional import LazyObject, Promise
from django.utils.version import get_docs_version
from django.utils.version import PY311, get_docs_version
class BaseSerializer:
@ -125,8 +125,21 @@ class EnumSerializer(BaseSerializer):
def serialize(self):
enum_class = self.value.__class__
module = enum_class.__module__
if issubclass(enum_class, enum.Flag):
if PY311:
members = list(self.value)
else:
members, _ = enum._decompose(enum_class, self.value)
members = reversed(members)
else:
members = (self.value,)
return (
"%s.%s[%r]" % (module, enum_class.__qualname__, self.value.name),
" | ".join(
[
f"{module}.{enum_class.__qualname__}[{item.name!r}]"
for item in members
]
),
{"import %s" % module},
)

View File

@ -192,7 +192,7 @@ Management Commands
Migrations
~~~~~~~~~~
* ...
* Migrations now support serialization of ``enum.Flag`` objects.
Models
~~~~~~

View File

@ -739,7 +739,7 @@ Django can serialize the following:
- ``datetime.date``, ``datetime.time``, and ``datetime.datetime`` instances
(include those that are timezone-aware)
- ``decimal.Decimal`` instances
- ``enum.Enum`` instances
- ``enum.Enum`` and ``enum.Flag`` instances
- ``uuid.UUID`` instances
- :func:`functools.partial` and :class:`functools.partialmethod` instances
which have serializable ``func``, ``args``, and ``keywords`` values.
@ -756,6 +756,10 @@ Django can serialize the following:
- Any class reference (must be in module's top-level scope)
- Anything with a custom ``deconstruct()`` method (:ref:`see below <custom-deconstruct-method>`)
.. versionchanged:: 4.2
Serialization support for ``enum.Flag`` was added.
Django cannot serialize:
- Nested classes

View File

@ -413,6 +413,14 @@ class WriterTests(SimpleTestCase):
"(2, migrations.test_writer.IntFlagEnum['B'])], "
"default=migrations.test_writer.IntFlagEnum['A'])",
)
self.assertSerializedResultEqual(
IntFlagEnum.A | IntFlagEnum.B,
(
"migrations.test_writer.IntFlagEnum['A'] | "
"migrations.test_writer.IntFlagEnum['B']",
{"import migrations.test_writer"},
),
)
def test_serialize_choices(self):
class TextChoices(models.TextChoices):