I have a model with an Enum column that uses a python Enum class
import enum
import sqlalchemy
# ...
class MyCoolEnum(str, enum.Enum):
A = "A"
B = "B"
C = "C"
class MyModel(Base):
__tablename__ = "mymodel"
cool_enum_val = Column(Enum(MyCoolEnum))
I need to get the class of the enum in runtime, how can I do that?
You can get the class by accessing enum_class
on the column's type, like so:
print(MyModel.cool_enum_val.type.enum_class)
# <enum 'MyCoolEnum'>
Alternatively, you can access the column dynamically with:
print(MyModel.__table__.columns['cool_enum_val'].type.enum_class)
# <enum 'MyCoolEnum'>