|
def | __init__ (self, typing.Union[SchemaABC, type, str, typing.Callable[[], SchemaABC]] nested, *typing.Any default=missing_, typing.Optional[types.StrSequenceOrSet] only=None, types.StrSequenceOrSet exclude=(), bool many=False, typing.Optional[str] unknown=None, **kwargs) |
|
def | schema (self) |
|
None | __init__ (self, *typing.Any default=missing_, typing.Any missing=missing_, typing.Optional[str] data_key=None, typing.Optional[str] attribute=None, typing.Optional[typing.Union[typing.Callable[[typing.Any], typing.Any], typing.Iterable[typing.Callable[[typing.Any], typing.Any]],]] validate=None, bool required=False, typing.Optional[bool] allow_none=None, bool load_only=False, bool dump_only=False, typing.Optional[typing.Dict[str, str]] error_messages=None, typing.Optional[typing.Mapping[str, typing.Any]] metadata=None, **additional_metadata) |
|
str | __repr__ (self) |
|
def | __deepcopy__ (self, memo) |
|
def | get_value (self, obj, attr, accessor=None, default=missing_) |
|
ValidationError | make_error (self, str key, **kwargs) |
|
def | fail (self, str key, **kwargs) |
|
def | serialize (self, str attr, typing.Any obj, typing.Optional[typing.Callable[[typing.Any, str, typing.Any], typing.Any]] accessor=None, **kwargs) |
|
def | deserialize (self, typing.Any value, typing.Optional[str] attr=None, typing.Optional[typing.Mapping[str, typing.Any]] data=None, **kwargs) |
|
def | context (self) |
|
def | serialize (self, attr, obj, accessor=None) |
|
def | deserialize (self, value) |
|
Allows you to nest a :class:`Schema <marshmallow.Schema>`
inside a field.
Examples: ::
class ChildSchema(Schema):
id = fields.Str()
name = fields.Str()
# Use lambda functions when you need two-way nesting or self-nesting
parent = fields.Nested(lambda: ParentSchema(only=("id",)), dump_only=True)
siblings = fields.List(fields.Nested(lambda: ChildSchema(only=("id", "name"))))
class ParentSchema(Schema):
id = fields.Str()
children = fields.List(
fields.Nested(ChildSchema(only=("id", "parent", "siblings")))
)
spouse = fields.Nested(lambda: ParentSchema(only=("id",)))
When passing a `Schema <marshmallow.Schema>` instance as the first argument,
the instance's ``exclude``, ``only``, and ``many`` attributes will be respected.
Therefore, when passing the ``exclude``, ``only``, or ``many`` arguments to `fields.Nested`,
you should pass a `Schema <marshmallow.Schema>` class (not an instance) as the first argument.
::
# Yes
author = fields.Nested(UserSchema, only=('id', 'name'))
# No
author = fields.Nested(UserSchema(), only=('id', 'name'))
:param nested: `Schema` instance, class, class name (string), or callable that returns a `Schema` instance.
:param exclude: A list or tuple of fields to exclude.
:param only: A list or tuple of fields to marshal. If `None`, all fields are marshalled.
This parameter takes precedence over ``exclude``.
:param many: Whether the field is a collection of objects.
:param unknown: Whether to exclude, include, or raise an error for unknown
fields in the data. Use `EXCLUDE`, `INCLUDE` or `RAISE`.
:param kwargs: The same keyword arguments that :class:`Field` receives.