Contents
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.
anyblok_marshmallow.fields.
Nested
(nested, default=<marshmallow.missing>, exclude=(), only=None, **kwargs)Bases: marshmallow.fields.Nested
Inherit marshmallow fields.Nested
context
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.
root
Reference to the Schema that this field belongs to even if it is buried in a 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, obj, accessor=None)Pulls the value for the given key from the object, applies the field’s formatting and returns the result.
Parameters: | |
---|---|
Raises: | ValidationError – In case of formatting problem |
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 |
---|
anyblok_marshmallow.schema.
format_fields
(x)remove the anyblok prefix form the field name
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
anyblok_marshmallow.schema.
ModelSchemaOpts
(meta, *args, **kwargs)Bases: marshmallow.schema.SchemaOpts
Model schema option for Model schema
Add get option from the Meta:
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
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 theadditional
and fields
are
mutually-exclusive options.include
: Dictionary of additional fields to include in the schema. It isexclude
: Tuple or list of fields to exclude in the serialized result.dateformat
: Date format for all DateTime fields that do not have theirstrict
: If True, raise errors during marshalling rather thanjson_module
: JSON module to use for loads and dumps.json
module in the stdlib.ordered
: If True, order serialization output according to theindex_errors
: If True, errors dictionaries will include the indexload_only
: Tuple or list of fields to exclude from serialized results.dump_only
: Tuple or list of fields to exclude from deserializationOPTIONS_CLASS
alias of ModelSchemaOpts
accessor
(func)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.
Parameters: |
|
---|---|
Returns: | A tuple of the form ( |
Return type: | MarshalResult, a collections.namedtuple |
New in version 1.0.0.
error_handler
(func)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.
Example:
class UserSchema(Schema):
email = fields.Email()
@UserSchema.error_handler
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_marsmallow_instance
()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.
Parameters: |
|
---|
New in version 2.0.0.
loads
(json_data, many=None, *args, **kwargs)Same as load()
, except it takes a JSON string as input.
Parameters: |
|
---|---|
Returns: | A tuple of the form ( |
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.
schema
property to get the real schema