pydantic

class litestar.plugins.pydantic.PydanticDIPlugin[原始碼]

基底類別:DIPlugin

get_typed_init(type_: Any) tuple[Signature, dict[str, Any]][原始碼]

Return signature and type information about the type_s __init__() method.

has_typed_init(type_: Any) bool[原始碼]

Return True if type_ has type information available for its __init__() method that cannot be extracted from this method's type annotations (e.g. a Pydantic BaseModel subclass), and DIPlugin.get_typed_init() supports extraction of these annotations.

class litestar.plugins.pydantic.PydanticDTO[原始碼]

基底類別: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.

decode_bytes(value: bytes) Any[原始碼]

Decode a byte string into an the DTO's datatype.

classmethod detect_nested_field(field_definition: FieldDefinition) bool[原始碼]

Return True if field_definition represents a nested model field.

參數:

field_definition -- inspect type to determine if field represents a nested model.

回傳:

True if field_definition represents a nested model field.

classmethod generate_field_definitions(model_type: type[pydantic_v1.BaseModel | pydantic_v2.BaseModel]) Generator[DTOFieldDefinition, None, None][原始碼]

Generate FieldDefinition instances from model_type.

產出:

FieldDefinition instances.

classmethod get_config_for_model_type(config: DTOConfig, model_type: type[Any]) DTOConfig[原始碼]

Create a new configuration for this specific model_type, during the creation of the factory.

The returned config object will be set as the config attribute on the newly defined factory class.

class litestar.plugins.pydantic.PydanticInitPlugin[原始碼]

基底類別: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[原始碼]

Pydantic Plugin to support serialization / validation of Pydantic types / models

參數:
  • 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 models

  • validate_strict -- Use strict=True when calling .model_validate on Pydantic 2.x models

  • round_trip -- use round_trip=True when calling .model_dump and .model_dump_json on Pydantic 2.x models

on_app_init(app_config: AppConfig) AppConfig[原始碼]

Receive the AppConfig instance after on_app_init hooks have been called.

範例

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()])
參數:

app_config -- The AppConfig instance.

回傳:

The app config object.

class litestar.plugins.pydantic.PydanticPlugin[原始碼]

基底類別:InitPlugin

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, round_trip: bool = False) None[原始碼]

Pydantic Plugin to support serialization / validation of Pydantic types / models

參數:
  • 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 models

  • validate_strict -- Use strict=True when calling .model_validate on Pydantic 2.x models

  • round_trip -- use round_trip=True when calling .model_dump and .model_dump_json on Pydantic 2.x models

on_app_init(app_config: AppConfig) AppConfig[原始碼]

Configure application for use with Pydantic.

參數:

app_config -- The AppConfig instance.

class litestar.plugins.pydantic.PydanticSchemaPlugin[原始碼]

基底類別:OpenAPISchemaPlugin

__init__(prefer_alias: bool = False) None[原始碼]
classmethod for_pydantic_model(field_definition: FieldDefinition, schema_creator: SchemaCreator) Schema | Reference[原始碼]

Create a schema object for a given pydantic model class.

參數:
  • field_definition -- FieldDefinition instance.

  • schema_creator -- An instance of the schema creator class

回傳:

A schema instance.

static is_constrained_field(field_definition: FieldDefinition) bool[原始碼]

Return True if the field should be treated as constrained. If returning True, constraints should be defined in the field's extras

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.

參數:

value -- An arbitrary value.

回傳:

A bool indicating whether the value is supported by the plugin.

static is_undefined_sentinel(value: Any) bool[原始碼]

Return True if value should be treated as an undefined field

to_openapi_schema(field_definition: FieldDefinition, schema_creator: SchemaCreator) Schema | Reference[原始碼]

Given a type annotation, transform it into an OpenAPI schema class.

參數:
  • field_definition -- FieldDefinition instance.

  • schema_creator -- An instance of the schema creator class

回傳:

An OpenAPI instance.