Exceptions

RegistryNotFound

exception anyblok_marshmallow.exceptions.RegistryNotFound

Bases: Exception

Exception raised when no registry is found to build schema

with_traceback()

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

Fields

Nested

class anyblok_marshmallow.fields.Nested(nested: Union[marshmallow.base.SchemaABC, type, str], *, default: Any = <marshmallow.missing>, only: Union[Sequence[str], Set[str]] = None, exclude: Union[Sequence[str], Set[str]] = (), many: bool = False, unknown: str = None, **kwargs)

Bases: marshmallow.fields.Nested

Inherit marshmallow fields.Nested

context

The context dictionary for the parent Schema.

deserialize(value: Any, attr: str = None, data: Mapping[str, Any] = None, **kwargs)

Deserialize value.

Parameters:
  • value – The value to deserialize.
  • attr – The attribute/key in data to deserialize.
  • data – The raw input data passed to Schema.load.
  • kwargs – Field-specific keyword arguments.
Raises:

ValidationError – If an invalid value is passed or if a required value is missing.

fail(key: str, **kwargs)

Helper method that raises a ValidationError with an error message from self.error_messages.

Deprecated since version 3.0.0: Use make_error <marshmallow.fields.Field.make_error> instead.

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

Return the value for a given key from an object.

Parameters:
  • obj (object) – The object to get the value from.
  • attr (str) – The attribute/key in obj to get the value from.
  • accessor (callable) – A callable used to retrieve the value of attr from the object obj. Defaults to marshmallow.utils.get_value.
make_error(key: str, **kwargs) → marshmallow.exceptions.ValidationError

Helper method to make a ValidationError with an error message from self.error_messages.

root

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

schema

Overload the super property to remove cache

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

serialize(attr: str, obj: Any, accessor: Callable[[Any, str, Any], Any] = None, **kwargs)

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

Parameters:
  • attr – The attribute/key to get from the object.
  • obj – The object to access the attribute/key from.
  • accessor – Function used to access values from obj.
  • kwargs – Field-specific keyword arguments.

File

class anyblok_marshmallow.fields.File(*, default: Any = <marshmallow.missing>, missing: Any = <marshmallow.missing>, data_key: str = None, attribute: str = None, validate: Union[Callable[[Any], Any], Sequence[Callable[[Any], Any]], Generator[Callable[[Any], Any], None, None]] = None, required: bool = False, allow_none: bool = None, load_only: bool = False, dump_only: bool = False, error_messages: Dict[str, str] = None, **metadata)

Bases: marshmallow.fields.Field

context

The context dictionary for the parent Schema.

deserialize(value: Any, attr: str = None, data: Mapping[str, Any] = None, **kwargs)

Deserialize value.

Parameters:
  • value – The value to deserialize.
  • attr – The attribute/key in data to deserialize.
  • data – The raw input data passed to Schema.load.
  • kwargs – Field-specific keyword arguments.
Raises:

ValidationError – If an invalid value is passed or if a required value is missing.

fail(key: str, **kwargs)

Helper method that raises a ValidationError with an error message from self.error_messages.

Deprecated since version 3.0.0: Use make_error <marshmallow.fields.Field.make_error> instead.

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

Return the value for a given key from an object.

Parameters:
  • obj (object) – The object to get the value from.
  • attr (str) – The attribute/key in obj to get the value from.
  • accessor (callable) – A callable used to retrieve the value of attr from the object obj. Defaults to marshmallow.utils.get_value.
make_error(key: str, **kwargs) → marshmallow.exceptions.ValidationError

Helper method to make a ValidationError with an error message from self.error_messages.

root

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

serialize(attr: str, obj: Any, accessor: Callable[[Any, str, Any], Any] = None, **kwargs)

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

Parameters:
  • attr – The attribute/key to get from the object.
  • obj – The object to access the attribute/key from.
  • accessor – Function used to access values from obj.
  • kwargs – Field-specific keyword arguments.

Text

class anyblok_marshmallow.fields.Text(*, default: Any = <marshmallow.missing>, missing: Any = <marshmallow.missing>, data_key: str = None, attribute: str = None, validate: Union[Callable[[Any], Any], Sequence[Callable[[Any], Any]], Generator[Callable[[Any], Any], None, None]] = None, required: bool = False, allow_none: bool = None, load_only: bool = False, dump_only: bool = False, error_messages: Dict[str, str] = None, **metadata)

Bases: marshmallow.fields.String

Simple field use to distinct by the class String and Text

context

The context dictionary for the parent Schema.

deserialize(value: Any, attr: str = None, data: Mapping[str, Any] = None, **kwargs)

Deserialize value.

Parameters:
  • value – The value to deserialize.
  • attr – The attribute/key in data to deserialize.
  • data – The raw input data passed to Schema.load.
  • kwargs – Field-specific keyword arguments.
Raises:

ValidationError – If an invalid value is passed or if a required value is missing.

fail(key: str, **kwargs)

Helper method that raises a ValidationError with an error message from self.error_messages.

Deprecated since version 3.0.0: Use make_error <marshmallow.fields.Field.make_error> instead.

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

Return the value for a given key from an object.

Parameters:
  • obj (object) – The object to get the value from.
  • attr (str) – The attribute/key in obj to get the value from.
  • accessor (callable) – A callable used to retrieve the value of attr from the object obj. Defaults to marshmallow.utils.get_value.
make_error(key: str, **kwargs) → marshmallow.exceptions.ValidationError

Helper method to make a ValidationError with an error message from self.error_messages.

root

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

serialize(attr: str, obj: Any, accessor: Callable[[Any, str, Any], Any] = None, **kwargs)

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

Parameters:
  • attr – The attribute/key to get from the object.
  • obj – The object to access the attribute/key from.
  • accessor – Function used to access values from obj.
  • kwargs – Field-specific keyword arguments.

JsonCollection

class anyblok_marshmallow.fields.JsonCollection(fieldname=None, keys=None, instance='default', cls_or_instance_type=<class 'marshmallow.fields.String'>, *args, **kwargs)

Bases: marshmallow.fields.Field

context

The context dictionary for the parent Schema.

deserialize(value: Any, attr: str = None, data: Mapping[str, Any] = None, **kwargs)

Deserialize value.

Parameters:
  • value – The value to deserialize.
  • attr – The attribute/key in data to deserialize.
  • data – The raw input data passed to Schema.load.
  • kwargs – Field-specific keyword arguments.
Raises:

ValidationError – If an invalid value is passed or if a required value is missing.

fail(key: str, **kwargs)

Helper method that raises a ValidationError with an error message from self.error_messages.

Deprecated since version 3.0.0: Use make_error <marshmallow.fields.Field.make_error> instead.

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

Return the value for a given key from an object.

Parameters:
  • obj (object) – The object to get the value from.
  • attr (str) – The attribute/key in obj to get the value from.
  • accessor (callable) – A callable used to retrieve the value of attr from the object obj. Defaults to marshmallow.utils.get_value.
make_error(key: str, **kwargs) → marshmallow.exceptions.ValidationError

Helper method to make a ValidationError with an error message from self.error_messages.

root

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

serialize(attr: str, obj: Any, accessor: Callable[[Any, str, Any], Any] = None, **kwargs)

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

Parameters:
  • attr – The attribute/key to get from the object.
  • obj – The object to access the attribute/key from.
  • accessor – Function used to access values from obj.
  • kwargs – Field-specific keyword arguments.

Country

class anyblok_marshmallow.fields.Country(mode=None, load_mode=<Modes.ALPHA_3: 'alpha_3'>, dump_mode=<Modes.ALPHA_3: 'alpha_3'>, *args, **kwargs)

Bases: marshmallow.fields.String

class Modes

Bases: enum.Enum

An enumeration.

context

The context dictionary for the parent Schema.

deserialize(value: Any, attr: str = None, data: Mapping[str, Any] = None, **kwargs)

Deserialize value.

Parameters:
  • value – The value to deserialize.
  • attr – The attribute/key in data to deserialize.
  • data – The raw input data passed to Schema.load.
  • kwargs – Field-specific keyword arguments.
Raises:

ValidationError – If an invalid value is passed or if a required value is missing.

fail(key: str, **kwargs)

Helper method that raises a ValidationError with an error message from self.error_messages.

Deprecated since version 3.0.0: Use make_error <marshmallow.fields.Field.make_error> instead.

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

Return the value for a given key from an object.

Parameters:
  • obj (object) – The object to get the value from.
  • attr (str) – The attribute/key in obj to get the value from.
  • accessor (callable) – A callable used to retrieve the value of attr from the object obj. Defaults to marshmallow.utils.get_value.
make_error(key: str, **kwargs) → marshmallow.exceptions.ValidationError

Helper method to make a ValidationError with an error message from self.error_messages.

root

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

serialize(attr: str, obj: Any, accessor: Callable[[Any, str, Any], Any] = None, **kwargs)

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

Parameters:
  • attr – The attribute/key to get from the object.
  • obj – The object to access the attribute/key from.
  • accessor – Function used to access values from obj.
  • kwargs – Field-specific keyword arguments.

InstanceField

class anyblok_marshmallow.fields.Country(mode=None, load_mode=<Modes.ALPHA_3: 'alpha_3'>, dump_mode=<Modes.ALPHA_3: 'alpha_3'>, *args, **kwargs)

Bases: marshmallow.fields.String

class Modes

Bases: enum.Enum

An enumeration.

context

The context dictionary for the parent Schema.

deserialize(value: Any, attr: str = None, data: Mapping[str, Any] = None, **kwargs)

Deserialize value.

Parameters:
  • value – The value to deserialize.
  • attr – The attribute/key in data to deserialize.
  • data – The raw input data passed to Schema.load.
  • kwargs – Field-specific keyword arguments.
Raises:

ValidationError – If an invalid value is passed or if a required value is missing.

fail(key: str, **kwargs)

Helper method that raises a ValidationError with an error message from self.error_messages.

Deprecated since version 3.0.0: Use make_error <marshmallow.fields.Field.make_error> instead.

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

Return the value for a given key from an object.

Parameters:
  • obj (object) – The object to get the value from.
  • attr (str) – The attribute/key in obj to get the value from.
  • accessor (callable) – A callable used to retrieve the value of attr from the object obj. Defaults to marshmallow.utils.get_value.
make_error(key: str, **kwargs) → marshmallow.exceptions.ValidationError

Helper method to make a ValidationError with an error message from self.error_messages.

root

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

serialize(attr: str, obj: Any, accessor: Callable[[Any, str, Any], Any] = None, **kwargs)

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

Parameters:
  • attr – The attribute/key to get from the object.
  • obj – The object to access the attribute/key from.
  • accessor – Function used to access values from obj.
  • kwargs – Field-specific keyword arguments.

Schema

update_from_kwargs

anyblok_marshmallow.schema.update_from_kwargs(*entries)

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

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

format_field

anyblok_marshmallow.schema.format_fields(x)

remove the anyblok prefix form the field name

ModelConverter

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

TemplateSchema

class anyblok_marshmallow.schema.TemplateSchema

Bases: object

Base class of Schema generated by SchemaWrapper

OPTIONS_CLASS

alias of marshmallow_sqlalchemy.schema.ModelSchemaOpts

PostLoadSchema

class anyblok_marshmallow.schema.PostLoadSchema

Bases: object

Return the AnyBlok instance from marshmallow deserialize

SchemaWrapper

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

Bases: marshmallow.base.SchemaABC

Schema Wrapper to generate marshmallow schema

class MySchema(SchemaWrapper):
    model = 'Model.Name'

the wrapper implement the marshmallow.base.SchemaABC abstract class. And call the methods of the marshmallow schema with the same parameter

Some class attributes can be added to improve the schema:

  • model: str, the registry name of the AnyBlok model
  • required_fields: list of the field which become required In this case the anyblok columns are not required but schema force them to be required
  • registry: the anyblok registry, only if you know it
  • only_primary_key: boolean, if True the marshmallow parameter only will be filled with the name of the primary keys.

Note

The model and registry are required to generate the schema. they can be defined by class attribute, parameter in the methods (load, loads, validate, dump, dumps) or in the context attribute.

generate_marsmallow_instance

Generate the real mashmallow-sqlalchemy schema

schema

property to get the real schema