Using context¶
Warning
The context
attribute is deprecated and will be removed in marshmallow 4.
Use contextvars.ContextVar
for passing context to fields, pre-/post-processing methods, and validators instead.
marshmallow 4 will also provide an experimental helper API
for using context.
The context
attribute of a Schema
is a general-purpose store for extra information that may be needed for (de)serialization.
It may be used in both Schema
and Field
methods.
schema = UserSchema()
# Make current HTTP request available to
# custom fields, schema methods, schema validators, etc.
schema.context["request"] = request
schema.dump(user)