|
def | __init__ (self, *typing.Optional[types.StrSequenceOrSet] only=None, types.StrSequenceOrSet exclude=(), bool many=False, typing.Optional[typing.Dict] context=None, types.StrSequenceOrSet load_only=(), types.StrSequenceOrSet dump_only=(), typing.Union[bool, types.StrSequenceOrSet] partial=False, typing.Optional[str] unknown=None) |
|
str | __repr__ (self) |
|
type | dict_class (self) |
|
type | set_class (self) |
|
type | from_dict (cls, typing.Dict[str, typing.Union[ma_fields.Field, type]] fields, *str name="GeneratedSchema") |
|
def | handle_error (self, ValidationError error, typing.Any data, *bool many, **kwargs) |
| Override-able methods #####. More...
|
|
def | get_attribute (self, typing.Any obj, str attr, typing.Any default) |
|
def | dump (self, typing.Any obj, *typing.Optional[bool] many=None) |
|
def | dumps (self, typing.Any obj, *args, typing.Optional[bool] many=None, **kwargs) |
|
def | load (self, typing.Union[typing.Mapping[str, typing.Any], typing.Iterable[typing.Mapping[str, typing.Any]],] data, *typing.Optional[bool] many=None, typing.Optional[typing.Union[bool, types.StrSequenceOrSet]] partial=None, typing.Optional[str] unknown=None) |
|
def | loads (self, str json_data, *typing.Optional[bool] many=None, typing.Optional[typing.Union[bool, types.StrSequenceOrSet]] partial=None, typing.Optional[str] unknown=None, **kwargs) |
|
typing.Dict[str, typing.List[str]] | validate (self, typing.Mapping data, *typing.Optional[bool] many=None, typing.Optional[typing.Union[bool, types.StrSequenceOrSet]] partial=None) |
|
None | on_bind_field (self, str field_name, ma_fields.Field field_obj) |
|
def | dump (self, obj, *typing.Optional[bool] many=None) |
|
def | dumps (self, obj, *typing.Optional[bool] many=None) |
|
def | load (self, data, *typing.Optional[bool] many=None, partial=None, unknown=None) |
|
def | loads (self, json_data, *typing.Optional[bool] many=None, partial=None, unknown=None, **kwargs) |
|
def | __new__ (mcs, name, bases, attrs) |
|
def | get_declared_fields (mcs, type klass, typing.List cls_fields, typing.List inherited_fields, type dict_cls) |
|
def | __init__ (cls, name, bases, attrs) |
|
typing.Dict[types.Tag, typing.List[str]] | resolve_hooks (cls) |
|
Base schema class with which to define custom schemas.
Example usage:
.. code-block:: python
import datetime as dt
from dataclasses import dataclass
from marshmallow import Schema, fields
@dataclass
class Album:
title: str
release_date: dt.date
class AlbumSchema(Schema):
title = fields.Str()
release_date = fields.Date()
album = Album("Beggars Banquet", dt.date(1968, 12, 6))
schema = AlbumSchema()
data = schema.dump(album)
data # {'release_date': '1968-12-06', 'title': 'Beggars Banquet'}
:param only: Whitelist of the declared fields to select when
instantiating the Schema. If None, all fields are used. Nested fields
can be represented with dot delimiters.
:param exclude: Blacklist of the declared fields to exclude
when instantiating the Schema. If a field appears in both `only` and
`exclude`, it is not used. Nested fields can be represented with dot
delimiters.
:param many: Should be set to `True` if ``obj`` is a collection
so that the object will be serialized to a list.
:param context: Optional context passed to :class:`fields.Method` and
:class:`fields.Function` fields.
:param load_only: Fields to skip during serialization (write-only fields)
:param dump_only: Fields to skip during deserialization (read-only fields)
:param partial: Whether to ignore missing fields and not require
any fields declared. Propagates down to ``Nested`` fields as well. If
its value is an iterable, only missing fields listed in that iterable
will be ignored. Use dot delimiters to specify nested fields.
:param unknown: Whether to exclude, include, or raise an error for unknown
fields in the data. Use `EXCLUDE`, `INCLUDE` or `RAISE`.
.. versionchanged:: 3.0.0
`prefix` parameter removed.
.. versionchanged:: 2.0.0
`__validators__`, `__preprocessors__`, and `__data_handlers__` are removed in favor of
`marshmallow.decorators.validates_schema`,
`marshmallow.decorators.pre_load` and `marshmallow.decorators.post_dump`.
`__accessor__` and `__error_handler__` are deprecated. Implement the
`handle_error` and `get_attribute` methods instead.
def load |
( |
|
self, |
|
|
typing.Union[
typing.Mapping[str, typing.Any],
typing.Iterable[typing.Mapping[str, typing.Any]],
] |
data, |
|
|
*typing.Optional[bool] |
many = None , |
|
|
typing.Optional[typing.Union[bool, types.StrSequenceOrSet]] |
partial = None , |
|
|
typing.Optional[str] |
unknown = None |
|
) |
| |
Deserialize a data structure to an object defined by this Schema's fields.
:param data: The data to deserialize.
:param many: Whether to deserialize `data` as a collection. If `None`, the
value for `self.many` is used.
:param partial: Whether to ignore missing fields and not require
any fields declared. Propagates down to ``Nested`` fields as well. If
its value is an iterable, only missing fields listed in that iterable
will be ignored. Use dot delimiters to specify nested fields.
:param unknown: Whether to exclude, include, or raise an error for unknown
fields in the data. Use `EXCLUDE`, `INCLUDE` or `RAISE`.
If `None`, the value for `self.unknown` is used.
:return: Deserialized data
.. versionadded:: 1.0.0
.. versionchanged:: 3.0.0b7
This method returns the deserialized data rather than a ``(data, errors)`` duple.
A :exc:`ValidationError <marshmallow.exceptions.ValidationError>` is raised
if invalid data are passed.
def loads |
( |
|
self, |
|
|
str |
json_data, |
|
|
*typing.Optional[bool] |
many = None , |
|
|
typing.Optional[typing.Union[bool, types.StrSequenceOrSet]] |
partial = None , |
|
|
typing.Optional[str] |
unknown = None , |
|
|
** |
kwargs |
|
) |
| |
Same as :meth:`load`, except it takes a JSON string as input.
:param json_data: A JSON string of the data to deserialize.
:param many: Whether to deserialize `obj` as a collection. If `None`, the
value for `self.many` is used.
:param partial: Whether to ignore missing fields and not require
any fields declared. Propagates down to ``Nested`` fields as well. If
its value is an iterable, only missing fields listed in that iterable
will be ignored. Use dot delimiters to specify nested fields.
:param unknown: Whether to exclude, include, or raise an error for unknown
fields in the data. Use `EXCLUDE`, `INCLUDE` or `RAISE`.
If `None`, the value for `self.unknown` is used.
:return: Deserialized data
.. versionadded:: 1.0.0
.. versionchanged:: 3.0.0b7
This method returns the deserialized data rather than a ``(data, errors)`` duple.
A :exc:`ValidationError <marshmallow.exceptions.ValidationError>` is raised
if invalid data are passed.