Skip to main content

model_serializer

No overview available.

def model_serializer(
mode: Literal['plain'] = 'plain',
when_used: WhenUsed = 'always',
return_type: Any = ...
) - > Callable[[_ModelPlainSerializerT], _ModelPlainSerializerT]

Decorates a method to define a custom serialization logic for a Pydantic model, allowing control over how the model is converted to a plain Python object.

Parameters

NameTypeDescription
modeLiteral['plain'] = 'plain'The serialization mode to use; 'plain' indicates the function will handle the entire serialization process for the model.
when_usedWhenUsed = 'always'Determines the conditions under which this serializer is applied, such as only when the model is a field in another model or always.
return_typeAny = ...The expected type of the serialized output, used for validation and schema generation.

Returns

TypeDescription
Callable[[_ModelPlainSerializerT], _ModelPlainSerializerT]A decorator function that wraps the model's serialization method.