Nesting schemas#

class oblate.fields.Object(schema_cls, *, init_kwargs=..., **kwargs)#

Field that deserializes to a Schema instance.

This field is used for nested schemas in raw data. The first argument when initializing this field is the schema class that is accepted by the field. For example:

class Author(oblate.Schema):
    name = fields.String()
    rating = fields.Integer()

class Book(oblate.Schema):
    title = fields.String()
    author = fields.Object(Author)

data = {
    'title': 'A book title',
    'author': {
        'name': 'John',
        'rating': 10,
    }
}
book = Book(data)
print(book.author.name, 'has rating of', book.author.rating)  # John has rating of 10

Changed in version 1.1: Passing Schema instances directly is now supported other than raw data.

ERR_INVALID_DATATYPE#

Error code raised when invalid data type is given in raw data.

Parameters:
  • schema_cls (Type[Schema]) – The schema class that the field accepts.

  • init_kwargs (Mapping[str, Any]) –

    The mapping of keyword arguments that should be passed to schema_cls while initializing it.

    This parameter is only taken into account when raw data is being deserialized to schema_cls. If a schema_cls instance is passed, this parameter is ignored.

    New in version 1.1.