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
| Name | Type | Description |
|---|---|---|
| mode | Literal['plain'] = 'plain' | The serialization mode to use; 'plain' indicates the function will handle the entire serialization process for the model. |
| when_used | WhenUsed = '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_type | Any = ... | The expected type of the serialized output, used for validation and schema generation. |
Returns
| Type | Description |
|---|---|
Callable[[_ModelPlainSerializerT], _ModelPlainSerializerT] | A decorator function that wraps the model's serialization method. |