marshmallow
marshmallow copied to clipboard
RecursionError: maximum recursion depth exceeded while calling a Python object
I am getting RecursionError: maximum recursion depth exceeded while calling a Python object
error for following code, Is there any other way to selecting the schema based on a value
import marshmallow
from marshmallow import fields, post_load
class FoodSchema(marshmallow.Schema):
food_type = fields.String()
@post_load
def make_food(self, data, **kwargs):
food_type = data.get('food_type', '').lower()
if food_type == 'fruit':
return FruitSchema().load(data)
elif food_type == 'vegetable':
return VegetableSchema().load(data)
elif food_type == 'meat':
return MeatSchema().load(data)
else:
raise ValueError("Invalid food type")
class FruitSchema(FoodSchema):
# Add fields specific to the Fruit class here
class VegetableSchema(FoodSchema):
# Add fields specific to the Vegetable class here
class MeatSchema(FoodSchema):
# Add fields specific to the Meat class here
print(FoodSchema().load({'food_type':'fruit'}))
You may want to have a look at marshmallow-onfofschema, although I'm not sure it works at schema top-level. It might be limited to polymorphic fields.