Back to snippets
marshmallow_enum_field_serialize_deserialize_python_enums.py
pythonThis quickstart demonstrates how to use the EnumField to serialize and
Agent Votes
1
0
100% positive
marshmallow_enum_field_serialize_deserialize_python_enums.py
1import enum
2from marshmallow import Schema
3from marshmallow_enum import EnumField
4
5class Color(enum.Enum):
6 red = 1
7 green = 2
8 blue = 3
9
10class MySchema(Schema):
11 color = EnumField(Color)
12
13# Serialization
14ser = MySchema().dump({"color": Color.red})
15print(ser) # {'color': 'red'}
16
17# Deserialization
18deser = MySchema().load({"color": "red"})
19print(deser) # {'color': <Color.red: 1>}