I am building a REST API on top of Flask and Connexion using marshmallow-mongoengine and MongoEngine to work with MongoDB.
Problem: If my input data includes additional, unknown fields they are always excluded from the resulting object when loading the data with marshmallow-mongoengine; even if my MongoEngine document should allow them as it extends DynamicDocument
. For example,
# Imports
from marshmallow_mongoengine import ModelSchema
from mongoengine.document import DynamicDocument
from mongoengine.fields import StringField, UUIDField
from pprint import pprint
# MongoEngine document
class User(DynamicDocument):
id = UUIDField(required=True, binary=False, primary_key=True)
first_name = StringField(required=True)
last_name = StringField(required=True)
# Marshmallow schema with the Marshmallow-MongoEngine mapping
class UserSchema(ModelSchema):
class Meta:
model = User
# Usage
# Initialization
user_schema = UserSchema()
user_data = {
"id": "77640cb5-75c9-4794-9581-5d7e2009cb78",
"first_name": "John",
"last_name": "Doe",
"age": 50 # additional, unknown field
}
# Load user data
user = user_schema.load(data=user_data).data
pprint(user.to_mongo())
# {'_id': '77640cb5-75c9-4794-9581-5d7e2009cb78',
# 'first_name': 'John',
# 'last_name': 'Doe'}
# Dump user
data = user_schema.dump(obj=user).data
pprint(data)
# {'first_name': 'John',
# 'id': '77640cb5-75c9-4794-9581-5d7e2009cb78',
# 'last_name': 'Doe'}
As can be seen from the example, the age
field is excluded from the loaded user
object.
Question: How to include additional, unknown fields when loading data with marshmallow-mongoengine? What am I missing? Or maybe there is a better way to work with MongoDB. Either way, I would like to have control over the fields to be (de)serialized and to be able to return JSON similar to the one shown in the output of "Dump user", but with the age
field included.