Ticket #33916: enum_serializer.patch

File enum_serializer.patch, 1.3 KB (added by Willem Van Onsem, 2 years ago)

patch of the EnumSerializer

  • django/db/migrations/serializer.py

    diff --git a/django/db/migrations/serializer.py b/django/db/migrations/serializer.py
    index 38da1e6ecc..295afbfebf 100644
    a b class EnumSerializer(BaseSerializer):  
    125125    def serialize(self):
    126126        enum_class = self.value.__class__
    127127        module = enum_class.__module__
     128        if issubclass(enum_class, enum.Flag):
     129            members, __ = enum._decompose(enum_class, self.value)
     130        else:
     131            members = (self.value,)
    128132        return (
    129             "%s.%s[%r]" % (module, enum_class.__qualname__, self.value.name),
     133            " | ".join(f"{module}.{enum_class.__qualname__}[{item.name!r}]" for item in members),
    130134            {"import %s" % module},
    131135        )
    132136
    class FunctionTypeSerializer(BaseSerializer):  
    154158                "import %s" % module
    155159            }
    156160        # Further error checking
    157         if self.value.__name__ == "<lambda>":
     161        # use .co_name, since this can not be modified, whereas __name__ can be altered
     162        # https://stackoverflow.com/a/56451124/67579
     163        if self.value.__code__.co_name == "<lambda>":
    158164            raise ValueError("Cannot serialize function: lambda")
    159165        if self.value.__module__ is None:
    160166            raise ValueError("Cannot serialize function %r: No module" % self.value)
Back to Top