Schema
Definition of the GenericSchema
base class.
For details about the inherited methods and attributes, see the official
documentation of marshmallow.Schema
.
GenericSchema ¶
Bases: GenericInsightMixin1[Model]
, Schema
Generic schema parameterized by a Model
class.
Data will always be deserialized to instances of that Model
class.
Note
The Model
referred to throughout the documentation is a
type variable, not any concrete class. For more information about
type variables, see the "Generics" section in
PEP 484.
Registers a post_load
hook to pass validated data to the constructor
of the specified Model
.
Requires a specific (non-generic) class to be passed as the Model
type argument for deserialization to work properly:
class Foo: # Model
...
class FooSchema(GenericSchema[Foo]):
...
__init__ ¶
__init__(
*,
only: Union[Sequence[str], set[str], None] = None,
exclude: Union[Sequence[str], set[str]] = (),
context: Union[dict[str, Any], None] = None,
load_only: Union[Sequence[str], set[str]] = (),
dump_only: Union[Sequence[str], set[str]] = (),
partial: Union[bool, Sequence[str], set[str]] = False,
unknown: Optional[str] = None,
many: bool = False
) -> None
Emits a warning, if the many
argument is not False
.
Otherwise the same as in marshmallow.Schema
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
only |
Union[Sequence[str], set[str], None]
|
Whitelist of the declared fields to select when instantiating
the Schema. If |
None
|
exclude |
Union[Sequence[str], set[str]]
|
Blacklist of the declared fields to exclude when instantiating
the Schema. If a field appears in both |
()
|
context |
Union[dict[str, Any], None]
|
None
|
|
load_only |
Union[Sequence[str], set[str]]
|
Fields to skip during serialization (write-only fields) |
()
|
dump_only |
Union[Sequence[str], set[str]]
|
Fields to skip during deserialization (read-only fields) |
()
|
partial |
Union[bool, Sequence[str], set[str]]
|
Whether to ignore missing fields and not require any fields
declared. Propagates down to
|
False
|
unknown |
Optional[str]
|
Whether to exclude, include, or raise an error for unknown
fields in the data. Use |
None
|
many |
bool
|
False
|
__setattr__ ¶
__setattr__(name: str, value: Any) -> None
Warns, when trying to set many
to anything other than False
.
Otherwise the same the normal
object.__setattr__
.
dump ¶
dump(
obj: Union[Model, Iterable[Model]], *, many: Optional[bool] = None
) -> Union[dict[str, Any], list[dict[str, Any]]]
Serializes Model
objects to native Python data types.
Same as
marshmallow.Schema.dump
at runtime.
Annotations ensure that type checkers will infer the return type
correctly based on the many
argument, and also enforce the obj
argument to be an a list
of Model
instances, if many
is
set to True
or a single instance of it, if many
is False
(or omitted).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
obj |
Union[Model, Iterable[Model]]
|
The object or iterable of objects to serialize |
required |
many |
Optional[bool]
|
Whether to serialize |
None
|
Returns:
Type | Description |
---|---|
dict[str, Any]
|
if |
list[dict[str, Any]]
|
if |
dumps ¶
dumps(
obj: Union[Model, Iterable[Model]],
*args: Any,
many: Optional[bool] = None,
**kwargs: Any
) -> str
Same as dump
, but returns a JSON-encoded string.
instantiate ¶
instantiate(data: dict[str, Any], **_kwargs: Any) -> Model
Unpacks data
into the constructor of the specified Model
.
Registered as a
@post_load
hook for the schema.
Warning
You should probably not use this method directly. No parsing,
transformation or validation of any kind is done in this method.
The data
is passed to the Model
constructor "as is".
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data |
dict[str, Any]
|
The validated data after deserialization; will be unpacked
into the constructor of the specified |
required |
Returns:
Type | Description |
---|---|
Model
|
Instance of the schema's |
load ¶
load(
data: Union[Mapping[str, Any], Iterable[Mapping[str, Any]]],
*,
many: Optional[bool] = None,
partial: Union[bool, Sequence[str], set[str], None] = None,
unknown: Optional[str] = None
) -> Union[list[Model], Model]
Deserializes data to objects of the specified Model
class.
Same as
marshmallow.Schema.load
at
runtime, but data will always pass through the
instantiate
hook after deserialization.
Annotations ensure that type checkers will infer the return type
correctly based on the Model
type argument of the class.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data |
Union[Mapping[str, Any], Iterable[Mapping[str, Any]]]
|
The data to deserialize |
required |
many |
Optional[bool]
|
Whether to deserialize |
None
|
partial |
Union[bool, Sequence[str], set[str], None]
|
Whether to ignore missing fields and not require any
fields declared. Propagates down to
|
None
|
unknown |
Optional[str]
|
Whether to exclude, include, or raise an error for unknown
fields in the data. Use |
None
|
Returns:
Type | Description |
---|---|
Model
|
if |
list[Model]
|
if |
loads ¶
loads(
json_data: str,
*,
many: Optional[bool] = None,
partial: Union[bool, Sequence[str], set[str], None] = None,
unknown: Optional[str] = None,
**kwargs: Any
) -> Union[list[Model], Model]
Deserializes data to objects of the specified Model
class.
Same as
marshmallow.Schema.loads
at
runtime, but data will always pass through the
instantiate
hook after deserialization.
Annotations ensure that type checkers will infer the return type
correctly based on the Model
type argument of the class.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
json_data |
str
|
A JSON string of the data to deserialize |
required |
many |
Optional[bool]
|
Whether to deserialize |
None
|
partial |
Union[bool, Sequence[str], set[str], None]
|
Whether to ignore missing fields and not require any
fields declared. Propagates down to
|
None
|
unknown |
Optional[str]
|
Whether to exclude, include, or raise an error for unknown
fields in the data. Use |
None
|
**kwargs |
Any
|
Passed to the JSON decoder |
{}
|
Returns:
Type | Description |
---|---|
Model
|
if |
list[Model]
|
if |