{"library":"marshmallow-polyfield","title":"marshmallow-polyfield","description":"marshmallow-polyfield is an unofficial extension to Marshmallow (version 3+) that enables defining polymorphic fields within your schemas. It allows for serialization and deserialization of objects that can have different underlying types, mapping them to appropriate Marshmallow schemas based on a discriminator field. The current version is 5.11, and it maintains an active release cadence to ensure compatibility with recent Marshmallow versions.","language":"python","status":"active","last_verified":"Thu Apr 16","install":{"commands":["pip install marshmallow-polyfield"],"cli":null},"imports":["from marshmallow_polyfield import PolyField","from marshmallow_polyfield import PolySchema"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"from marshmallow import Schema, fields\nfrom marshmallow_polyfield import PolyField\n\nclass Dog:\n    def __init__(self, name, breed):\n        self.name = name\n        self.breed = breed\n        self.animal_type = 'dog'\n\nclass Cat:\n    def __init__(self, name, color):\n        self.name = name\n        self.color = color\n        self.animal_type = 'cat'\n\nclass DogSchema(Schema):\n    name = fields.String(required=True)\n    breed = fields.String(required=True)\n    animal_type = fields.Constant('dog')\n\nclass CatSchema(Schema):\n    name = fields.String(required=True)\n    color = fields.String(required=True)\n    animal_type = fields.Constant('cat')\n\nclass AnimalSchema(Schema):\n    animal = PolyField(\n        deserialization_schema_map={\n            'dog': DogSchema,\n            'cat': CatSchema\n        },\n        serialization_schema_map={\n            'dog': DogSchema,\n            'cat': CatSchema\n        },\n        lookup_field='animal_type'\n    )\n\n# --- Example Usage ---\ndog_obj = Dog(name='Buddy', breed='Golden Retriever')\ncat_obj = Cat(name='Whiskers', color='black')\n\n# Serialization\npoly_schema = AnimalSchema()\ndog_data = poly_schema.dump({'animal': dog_obj})\ncat_data = poly_schema.dump({'animal': cat_obj})\n\nprint(f\"Serialized Dog: {dog_data}\")\nprint(f\"Serialized Cat: {cat_data}\")\n\n# Deserialization\ndog_dict = {'animal_type': 'dog', 'name': 'Rex', 'breed': 'German Shepherd'}\ncat_dict = {'animal_type': 'cat', 'name': 'Mittens', 'color': 'white'}\n\ndeserialized_dog = poly_schema.load({'animal': dog_dict})\ndeserialized_cat = poly_schema.load({'animal': cat_dict})\n\nprint(f\"Deserialized Dog type: {type(deserialized_dog['animal'])}\")\nprint(f\"Deserialized Cat type: {type(deserialized_cat['animal'])}\")\n","lang":"python","description":"This quickstart demonstrates how to use `PolyField` to handle polymorphic objects. It defines `Dog` and `Cat` classes with corresponding `DogSchema` and `CatSchema`. The `AnimalSchema` then uses `PolyField` with `deserialization_schema_map`, `serialization_schema_map`, and a `lookup_field` to correctly map between object types and schemas during both serialization and deserialization. The `lookup_field` ('animal_type' in this case) is crucial for `marshmallow-polyfield` to determine which concrete schema to use.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}