The nested Schema object. .. versionchanged:: 1.0.0 Renamed from `serializer` to `schema`.
(self)
| 572 | |
| 573 | @property |
| 574 | def schema(self): |
| 575 | """The nested Schema object. |
| 576 | |
| 577 | .. versionchanged:: 1.0.0 |
| 578 | Renamed from `serializer` to `schema`. |
| 579 | """ |
| 580 | if not self._schema: |
| 581 | # Inherit context from parent. |
| 582 | context = getattr(self.parent, "context", {}) |
| 583 | if callable(self.nested) and not isinstance(self.nested, type): |
| 584 | nested = self.nested() |
| 585 | else: |
| 586 | nested = self.nested |
| 587 | if isinstance(nested, dict): |
| 588 | # defer the import of `marshmallow.schema` to avoid circular imports |
| 589 | from .schema import Schema |
| 590 | |
| 591 | nested = Schema.from_dict(nested) |
| 592 | |
| 593 | if isinstance(nested, SchemaABC): |
| 594 | self._schema = copy.copy(nested) |
| 595 | self._schema.context.update(context) |
| 596 | # Respect only and exclude passed from parent and re-initialize fields |
| 597 | set_class = self._schema.set_class |
| 598 | if self.only is not None: |
| 599 | if self._schema.only is not None: |
| 600 | original = self._schema.only |
| 601 | else: # only=None -> all fields |
| 602 | original = self._schema.fields.keys() |
| 603 | self._schema.only = set_class(self.only) & set_class(original) |
| 604 | if self.exclude: |
| 605 | original = self._schema.exclude |
| 606 | self._schema.exclude = set_class(self.exclude) | set_class(original) |
| 607 | self._schema._init_fields() |
| 608 | else: |
| 609 | if isinstance(nested, type) and issubclass(nested, SchemaABC): |
| 610 | schema_class = nested |
| 611 | elif not isinstance(nested, (str, bytes)): |
| 612 | raise ValueError( |
| 613 | "`Nested` fields must be passed a " |
| 614 | f"`Schema`, not {nested.__class__}." |
| 615 | ) |
| 616 | elif nested == "self": |
| 617 | schema_class = self.root.__class__ |
| 618 | else: |
| 619 | schema_class = class_registry.get_class(nested) |
| 620 | self._schema = schema_class( |
| 621 | many=self.many, |
| 622 | only=self.only, |
| 623 | exclude=self.exclude, |
| 624 | context=context, |
| 625 | load_only=self._nested_normalized_option("load_only"), |
| 626 | dump_only=self._nested_normalized_option("dump_only"), |
| 627 | ) |
| 628 | return self._schema |
| 629 | |
| 630 | def _nested_normalized_option(self, option_name: str) -> list[str]: |
| 631 | nested_field = f"{self.name}." |
nothing calls this directly
no test coverage detected