pydantic¶
- class litestar.plugins.pydantic.PydanticDIPlugin[source]¶
Bases:
DIPlugin- get_typed_init(type_: Any) tuple[Signature, dict[str, Any]][source]¶
Return signature and type information about the
type_s__init__()method.
- has_typed_init(type_: Any) bool[source]¶
Return
Trueiftype_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.
- class litestar.plugins.pydantic.PydanticDTO[source]¶
Bases:
AbstractDTO,Generic[T]Support for domain modelling with Pydantic.
- decode_builtins(value: dict[str, Any]) Any[source]¶
Decode a dictionary of Python values into an the DTO’s datatype.
- classmethod detect_nested_field(field_definition: FieldDefinition) bool[source]¶
Return
Trueiffield_definitionrepresents a nested model field.- Parameters:
field_definition¶ – inspect type to determine if field represents a nested model.
- Returns:
Trueiffield_definitionrepresents a nested model field.
- class litestar.plugins.pydantic.PydanticInitPlugin[source]¶
Bases:
InitPlugin- __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, round_trip: bool = False) None[source]¶
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
Noneexclude_unset¶ – Fields to exclude during serialization when they arenot set
include¶ – Fields to exclude during serialization
prefer_alias¶ – Use the
by_alias=Trueflag when dumping modelsvalidate_strict¶ – Use
strict=Truewhen calling.model_validateon Pydantic 2.x modelsround_trip¶ – use
round_trip=Truewhen calling.model_dumpand.model_dump_jsonon Pydantic 2.x models
- on_app_init(app_config: AppConfig) AppConfig[source]¶
Receive the
AppConfiginstance 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.PydanticPlugin[source]¶
Bases:
InitPluginA 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, round_trip: bool = False) None[source]¶
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
Noneexclude_unset¶ – Fields to exclude during serialization when they arenot set
include¶ – Fields to exclude during serialization
prefer_alias¶ – Use the
by_alias=Trueflag when dumping modelsvalidate_strict¶ – Use
strict=Truewhen calling.model_validateon Pydantic 2.x modelsround_trip¶ – use
round_trip=Truewhen calling.model_dumpand.model_dump_jsonon Pydantic 2.x models
- class litestar.plugins.pydantic.PydanticSchemaPlugin[source]¶
Bases:
OpenAPISchemaPlugin- classmethod for_pydantic_model(field_definition: FieldDefinition, schema_creator: SchemaCreator) Schema | Reference[source]¶
Create a schema object for a given pydantic model class.
- static is_constrained_field(field_definition: FieldDefinition) bool[source]¶
Return
Trueif the field should be treated as constrained. If returningTrue, constraints should be defined in the field’s extras
- static is_plugin_supported_type(value: Any) bool[source]¶
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[source]¶
Return
Trueifvalueshould be treated as an undefined field
- to_openapi_schema(field_definition: FieldDefinition, schema_creator: SchemaCreator) Schema | Reference[source]¶
Given a type annotation, transform it into an OpenAPI schema class.