restapi.models.schema.Schema.from_dict

classmethod Schema.from_dict(fields, *, name='GeneratedSchema')[source]

Generate a Schema class given a dictionary of fields.

from marshmallow import Schema, fields

PersonSchema = Schema.from_dict({"name": fields.Str()})
print(PersonSchema().load({"name": "David"}))  # => {'name': 'David'}

Generated schemas are not added to the class registry and therefore cannot be referred to by name in Nested fields.

Parameters:
  • fields (dict) – Dictionary mapping field names to field instances.

  • name (str) – Optional name for the class, which will appear in the repr for the class.

Return type:

type

Added in version 3.0.0.

Parameters:
  • fields (dict[str, Union[Field, type]])

  • name (str, default: 'GeneratedSchema')

Return type:

type