pydantic#
- class litestar.contrib.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.contrib.pydantic.PydanticInitPlugin#
Bases:
InitPluginProtocol
- 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()])
- Parameters:
app_config – The
AppConfig
instance.- Returns:
The app config object.
- class litestar.contrib.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.
- Parameters:
value – An arbitrary value.
- Returns:
A typeguard dictating 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.
- Parameters:
field_definition – FieldDefinition instance.
schema_creator – An instance of the schema creator class
- Returns:
An
OpenAPI
instance.
- classmethod for_pydantic_model(field_definition: FieldDefinition, schema_creator: SchemaCreator) Schema #
Create a schema object for a given pydantic model class.
- Parameters:
field_definition – FieldDefinition instance.
schema_creator – An instance of the schema creator class
- Returns:
A schema instance.
- class litestar.contrib.pydantic.PydanticPlugin#
Bases:
InitPluginProtocol
A plugin that provides Pydantic integration.