pydantic#
- class litestar.plugins.pydantic.PydanticDTO#
Bases:
AbstractDTO
,Generic
[T
]Support for domain modelling with Pydantic.
- decode_builtins(value: dict[str, Any]) Any #
Decode a dictionary of Python values into an the DTO’s datatype.
- classmethod generate_field_definitions(model_type: type[pydantic_v1.BaseModel | pydantic_v2.BaseModel]) Generator[DTOFieldDefinition, None, None] #
Generate
FieldDefinition
instances frommodel_type
.- Yields:
FieldDefinition
instances.
- classmethod detect_nested_field(field_definition: FieldDefinition) bool #
Return
True
iffield_definition
represents a nested model field.- Parameters:
field_definition¶ – inspect type to determine if field represents a nested model.
- Returns:
True
iffield_definition
represents a nested model field.
- class litestar.plugins.pydantic.PydanticInitPlugin#
Bases:
InitPluginProtocol
- __init__(exclude: PydanticV1FieldsListType | PydanticV2FieldsListType | None = None, exclude_defaults: bool = False, exclude_none: bool = False, exclude_unset: bool = False, include: PydanticV1FieldsListType | PydanticV2FieldsListType | None = None, prefer_alias: bool = False, validate_strict: bool = False) None #
Pydantic Plugin to support serialization / validation of Pydantic types / models
- Parameters:
exclude¶ – Fields to exclude during serialization
exclude_defaults¶ – Fields to exclude during serialization when they are set to their default value
exclude_none¶ – Fields to exclude during serialization when they are set to
None
exclude_unset¶ – Fields to exclude during serialization when they arenot set
include¶ – Fields to exclude during serialization
prefer_alias¶ – Use the
by_alias=True
flag when dumping modelsvalidate_strict¶ – Use
strict=True
when calling.model_validate
on Pydantic 2.x models
- on_app_init(app_config: AppConfig) AppConfig #
Receive the
AppConfig
instance after on_app_init hooks have been called.Examples
from litestar import Litestar, get from litestar.di import Provide from litestar.plugins import InitPluginProtocol def get_name() -> str: return "world" @get("/my-path") def my_route_handler(name: str) -> dict[str, str]: return {"hello": name} class MyPlugin(InitPluginProtocol): def on_app_init(self, app_config: AppConfig) -> AppConfig: app_config.dependencies["name"] = Provide(get_name) app_config.route_handlers.append(my_route_handler) return app_config app = Litestar(plugins=[MyPlugin()])
- class litestar.plugins.pydantic.PydanticSchemaPlugin#
Bases:
OpenAPISchemaPlugin
- static is_plugin_supported_type(value: Any) bool #
Given a value of indeterminate type, determine if this value is supported by the plugin.
This is called by the default implementation of
is_plugin_supported_field()
for backwards compatibility. User’s should prefer to override that method instead.- Parameters:
value¶ – An arbitrary value.
- Returns:
A bool indicating whether the value is supported by the plugin.
- static is_undefined_sentinel(value: Any) bool #
Return
True
ifvalue
should be treated as an undefined field
- static is_constrained_field(field_definition: FieldDefinition) bool #
Return
True
if the field should be treated as constrained. If returningTrue
, constraints should be defined in the field’s extras
- to_openapi_schema(field_definition: FieldDefinition, schema_creator: SchemaCreator) Schema #
Given a type annotation, transform it into an OpenAPI schema class.
- classmethod for_pydantic_model(field_definition: FieldDefinition, schema_creator: SchemaCreator) Schema #
Create a schema object for a given pydantic model class.
- class litestar.plugins.pydantic.PydanticPlugin#
Bases:
InitPluginProtocol
A plugin that provides Pydantic integration.
- __init__(exclude: PydanticV1FieldsListType | PydanticV2FieldsListType | None = None, exclude_defaults: bool = False, exclude_none: bool = False, exclude_unset: bool = False, include: PydanticV1FieldsListType | PydanticV2FieldsListType | None = None, prefer_alias: bool = False, validate_strict: bool = False) None #
Pydantic Plugin to support serialization / validation of Pydantic types / models
- Parameters:
exclude¶ – Fields to exclude during serialization
exclude_defaults¶ – Fields to exclude during serialization when they are set to their default value
exclude_none¶ – Fields to exclude during serialization when they are set to
None
exclude_unset¶ – Fields to exclude during serialization when they arenot set
include¶ – Fields to exclude during serialization
prefer_alias¶ – Use the
by_alias=True
flag when dumping modelsvalidate_strict¶ – Use
strict=True
when calling.model_validate
on Pydantic 2.x models
- class litestar.plugins.pydantic.PydanticDIPlugin#
Bases:
DIPlugin
- has_typed_init(type_: Any) bool #
Return
True
iftype_
has type information available for its__init__()
method that cannot be extracted from this method’s type annotations (e.g. a Pydantic BaseModel subclass), andDIPlugin.get_typed_init()
supports extraction of these annotations.
- get_typed_init(type_: Any) tuple[inspect.Signature, dict[str, Any]] #
Return signature and type information about the
type_
s__init__()
method.