base_dto

class litestar.dto.base_dto.AbstractDTO[源代码]

基类:Generic[T]

Base class for DTO types.

config: ClassVar[DTOConfig]

Config objects to define properties of the DTO.

model_type: type[T]

If annotation is an iterable, this is the inner type, otherwise will be the same as annotation.

attribute_accessor()

getattr() like callable to access attributes on the data source

__init__(asgi_connection: ASGIConnection) None[源代码]

Create an AbstractDTOFactory type.

参数:

asgi_connection -- A ASGIConnection instance.

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.

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.

abstractmethod classmethod generate_field_definitions(model_type: type[Any]) Generator[DTOFieldDefinition, None, None][源代码]

Generate FieldDefinition instances from model_type.

生成器:

FieldDefinition instances.

abstractmethod 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 is_supported_model_type_field(field_definition: FieldDefinition) bool[源代码]

Check support for the given type.

参数:

field_definition -- A FieldDefinition instance.

返回:

Whether the type of the field definition is supported by the DTO.

classmethod create_for_field_definition(field_definition: FieldDefinition, handler_id: str, backend_cls: type[DTOBackend] | None = None) None[源代码]

Creates a DTO subclass for a field definition.

参数:
  • field_definition -- A FieldDefinition instance.

  • handler_id -- ID of the route handler for which to create a DTO instance.

  • backend_cls -- Alternative DTO backend class to use

返回:

None

classmethod create_openapi_schema(field_definition: FieldDefinition, handler_id: str, schema_creator: SchemaCreator) Reference | Schema[源代码]

Create an OpenAPI request body.

参数:
  • field_definition -- A parsed type annotation that represents the annotation used on the handler.

  • handler_id -- ID of the route handler for which to create a DTO instance.

  • schema_creator -- A factory for creating schemas. Has a for_field_definition() method that accepts a FieldDefinition instance.

返回:

OpenAPI request body.

classmethod resolve_generic_wrapper_type(field_definition: FieldDefinition) tuple[FieldDefinition, FieldDefinition, str] | None[源代码]

Handle where DTO supported data is wrapped in a generic container type.

参数:

field_definition -- A parsed type annotation that represents the annotation used to narrow the DTO type.

返回:

The data model type.

static get_model_type_hints(model_type: type[Any], namespace: dict[str, Any] | None = None) dict[str, FieldDefinition][源代码]

Retrieve type annotations for model_type.

参数:
  • model_type -- Any type-annotated class.

  • namespace -- Optional namespace to use for resolving type hints.

返回:

Parsed type hints for model_type resolved within the scope of its module.

static get_dto_config_from_annotated_type(field_definition: FieldDefinition) DTOConfig | None[源代码]

Extract data type and config instances from Annotated annotation.

参数:

field_definition -- A parsed type annotation that represents the annotation used to narrow the DTO type.

返回:

The type and config object extracted from the annotation.

classmethod resolve_model_type(field_definition: FieldDefinition) FieldDefinition[源代码]

Resolve the data model type from a parsed type.

参数:

field_definition -- A parsed type annotation that represents the annotation used to narrow the DTO type.

返回:

A FieldDefinition that represents the data model type.