2. API Reference

class marshmallow_mongoengine.ModelSchema(*, only: types.StrSequenceOrSet | None = None, exclude: types.StrSequenceOrSet = (), many: bool = False, context: dict | None = None, load_only: types.StrSequenceOrSet = (), dump_only: types.StrSequenceOrSet = (), partial: bool | types.StrSequenceOrSet | None = None, unknown: str | None = None)[source]

Base class for Mongoengine model-based Schemas.

Example:

from marshmallow_mongoengine import ModelSchema
from mymodels import User

class UserSchema(ModelSchema):
    class Meta:
        model = User
OPTIONS_CLASS

alias of SchemaOpts

update(obj, data)[source]

Helper function to update an already existing document instead of creating a new one. :param obj: Mongoengine Document to update :param data: incomming payload to deserialize :return: an :class UnmarshallResult:

Example:

from marshmallow_mongoengine import ModelSchema
from mymodels import User

class UserSchema(ModelSchema):
    class Meta:
        model = User

def update_obj(id, payload):
    user = User.objects(id=id).first()
    result = UserSchema().update(user, payload)
    result.data is user # True

Note:

Given the update is done on a existing object, the required param on the fields is ignored
class marshmallow_mongoengine.SchemaOpts(meta, *args, **kwargs)[source]

Options class for ModelSchema. Adds the following options:

  • model: The Mongoengine Document model to generate the Schema
    from (required).
  • model_fields_kwargs: Dict of {field: kwargs} to provide as
    additionals argument during fields creation.
  • model_build_obj: If true, :Schema load: returns a :model: objects
    instead of a dict (default: True).
  • model_converter: ModelConverter class to use for converting the
    Mongoengine Document model to marshmallow fields.
  • model_dump_only_pk: If the document autogenerate it primary_key
    (default behaviour in Mongoengine), ignore it from the incomming data (default: False)
  • model_skip_values: Skip the field if it contains one of the given
    values (default: None, [] and {})
class marshmallow_mongoengine.ModelConverter[source]

Class that converts a mongoengine Document into a dictionary of corresponding marshmallow Fields <marshmallow.fields.Field>.

exception marshmallow_mongoengine.ModelConversionError[source]

Raised when an error occurs in converting a Mongoengine construct to a marshmallow object.

marshmallow_mongoengine.register_field_builder(mongo_field_cls, builder)[source]

Register a :class MetaFieldBuilder: to a given Mongoengine Field :param mongo_field_cls: Mongoengine Field :param build: field_builder to register

marshmallow_mongoengine.register_field(mongo_field_cls, marshmallow_field_cls, available_params=())[source]

Bind a marshmallow field to it corresponding mongoengine field :param mongo_field_cls: Mongoengine Field :param marshmallow_field_cls: Marshmallow Field :param available_params: List of :class marshmallow_mongoengine.conversion.params.MetaParam:

instances to import the mongoengine field config to marshmallow