exception anyblok_marshmallow.exceptions.RegistryNotFound

Bases: Exception

Exception raised when no registry is found to build schema


Exception.with_traceback(tb) – set self.__traceback__ to tb and return self.



class anyblok_marshmallow.fields.Nested(nested, default=<marshmallow.missing>, exclude=(), only=None, **kwargs)

Bases: marshmallow.fields.Nested

Inherit marshmallow fields.Nested


The context dictionary for the parent Schema.

deserialize(value, attr=None, data=None)

Deserialize value.

Raises:ValidationError – If an invalid value is passed or if a required value is missing.
fail(key, **kwargs)

A helper method that simply raises a ValidationError.

get_value(attr, obj, accessor=None, default=<marshmallow.missing>)

Return the value for a given key from an object.


Reference to the Schema that this field belongs to even if it is buried in a List. Return None for unbound fields.


Overload the super property to remove cache

it is the only way to propagate the context at each call

serialize(attr, obj, accessor=None)

Pulls the value for the given key from the object, applies the field’s formatting and returns the result.

  • attr (str) – The attibute or key to get from the object.
  • obj (str) – The object to pull the key from.
  • accessor (callable) – Function used to pull values from obj.

ValidationError – In case of formatting problem




decorator to get temporaly the value in kwargs and put it in schema

Params entries:array ok entry name to take from the kwargs



remove the anyblok prefix form the field name


class anyblok_marshmallow.schema.ModelConverter(schema_cls=None)

Bases: marshmallow_sqlalchemy.convert.ModelConverter

Overwrite the ModelConverter class of marshmallow-sqlalchemy

The goal if to fix the fieldname, because they are prefixed.

fields_for_model(model, **kwargs)

Overwrite the method and remove prefix of the field name


class anyblok_marshmallow.schema.ModelSchemaOpts(meta, *args, **kwargs)

Bases: marshmallow.schema.SchemaOpts

Model schema option for Model schema

Add get option from the Meta:

  • model: name of an AnyBlok model required
  • registry: an AnyBlok registry
  • post_load_return_instance: return an instance object


class anyblok_marshmallow.schema.ModelSchema(*args, **kwargs)

Bases: marshmallow.schema.Schema

A marshmallow schema based on the AnyBlok Model

Wrap the real schema, because at the instanciation the registry is not available

class Meta

Bases: object

Options object for a Schema.

Example usage:

class Meta:
    fields = ("id", "email", "date_created")
    exclude = ("password", "secret_attribute")

Available options:

  • fields: Tuple or list of fields to include in the serialized result.
  • additional: Tuple or list of fields to include in addition to the
    explicitly declared fields. additional and fields are mutually-exclusive options.
  • include: Dictionary of additional fields to include in the schema. It is
    usually better to define fields as class variables, but you may need to use this option, e.g., if your fields are Python keywords. May be an OrderedDict.
  • exclude: Tuple or list of fields to exclude in the serialized result.
    Nested fields can be represented with dot delimiters.
  • dateformat: Date format for all DateTime fields that do not have their
    date format explicitly specified.
  • strict: If True, raise errors during marshalling rather than
    storing them.
  • json_module: JSON module to use for loads and dumps.
    Defaults to the json module in the stdlib.
  • ordered: If True, order serialization output according to the
    order in which fields were declared. Output of Schema.dump will be a collections.OrderedDict.
  • index_errors: If True, errors dictionaries will include the index
    of invalid items in a collection.
  • load_only: Tuple or list of fields to exclude from serialized results.
  • dump_only: Tuple or list of fields to exclude from deserialization

alias of ModelSchemaOpts


Decorator that registers a function for pulling values from an object to serialize. The function receives the Schema instance, the key of the value to get, the obj to serialize, and an optional default value.

Deprecated since version 2.0.0: Set the error_handler class Meta option instead.

dumps(obj, many=None, update_fields=True, *args, **kwargs)

Same as dump(), except return a JSON-encoded string.

  • obj – The object to serialize.
  • many (bool) – Whether to serialize obj as a collection. If None, the value for self.many is used.
  • update_fields (bool) – Whether to update the schema’s field classes. Typically set to True, but may be False when serializing a homogenous collection. This parameter is used by fields.Nested to avoid multiple updates.

A tuple of the form (data, errors)

Return type:

MarshalResult, a collections.namedtuple

New in version 1.0.0.


Decorator that registers an error handler function for the schema. The function receives the Schema instance, a dictionary of errors, and the serialized object (if serializing data) or data dictionary (if deserializing data) as arguments.


class UserSchema(Schema):
    email = fields.Email()

def handle_errors(schema, errors, obj):
    raise ValueError('An error occurred while marshalling {}'.format(obj))

user = User(email='invalid')
UserSchema().dump(user)  # => raises ValueError
UserSchema().load({'email': 'bademail'})  # raises ValueError

New in version 0.7.0.

Deprecated since version 2.0.0: Set the error_handler class Meta option instead.


Generate the real mashmallow-sqlalchemy schema

get_attribute(attr, obj, default)

Defines how to pull values from an object to serialize.

New in version 2.0.0.

handle_error(error, data)

Custom error handler function for the schema.

  • error (ValidationError) – The ValidationError raised during (de)serialization.
  • data – The original input data.

New in version 2.0.0.

loads(json_data, many=None, *args, **kwargs)

Same as load(), except it takes a JSON string as input.

  • json_data (str) – A JSON string of the data to deserialize.
  • many (bool) – Whether to deserialize obj as a collection. If None, the value for self.many is used.
  • partial (bool|tuple) – Whether to ignore missing fields. If None, the value for self.partial is used. If its value is an iterable, only missing fields listed in that iterable will be ignored.

A tuple of the form (data, errors)

Return type:

UnmarshalResult, a collections.namedtuple

New in version 1.0.0.

on_bind_field(field_name, field_obj)

Hook to modify a field when it is bound to the Schema. No-op by default.


property to get the real schema