dto#
Application DTO configuration
- class app.lib.dto.DTOConfig(exclude: AbstractSet[str] = <factory>, include: AbstractSet[str] = <factory>, rename_fields: dict[str, str] = <factory>, rename_strategy: RenameStrategy | None = None, max_nested_depth: int = 1, partial: bool = False, underscore_fields_private: bool = True, experimental_codegen_backend: bool | None = None, forbid_unknown_fields: bool = False)[source]#
Bases:
object
Control the generated DTO.
- exclude: AbstractSet[str]#
Explicitly exclude fields from the generated DTO.
If exclude is specified, all fields not specified in exclude will be included by default.
Notes
- The field names are dot-separated paths to nested fields, e.g.
"address.street"
will exclude the
"street"
field from a nested"address"
model.
- The field names are dot-separated paths to nested fields, e.g.
- ‘exclude’ mutually exclusive with ‘include’ - specifying both values will raise an
ImproperlyConfiguredException
.
- include: AbstractSet[str]#
Explicitly include fields in the generated DTO.
If include is specified, all fields not specified in include will be excluded by default.
Notes
- The field names are dot-separated paths to nested fields, e.g.
"address.street"
will include the
"street"
field from a nested"address"
model.
- The field names are dot-separated paths to nested fields, e.g.
- ‘include’ mutually exclusive with ‘exclude’ - specifying both values will raise an
ImproperlyConfiguredException
.
- rename_strategy: RenameStrategy | None = None#
Rename all fields using a pre-defined strategy or a custom strategy.
The pre-defined strategies are: upper, lower, camel, pascal.
A custom strategy is any callable that accepts a string as an argument and return a string.
Fields defined in
rename_fields
are ignored.
- __init__(exclude: AbstractSet[str] = <factory>, include: AbstractSet[str] = <factory>, rename_fields: dict[str, str] = <factory>, rename_strategy: RenameStrategy | None = None, max_nested_depth: int = 1, partial: bool = False, underscore_fields_private: bool = True, experimental_codegen_backend: bool | None = None, forbid_unknown_fields: bool = False) None #
- class app.lib.dto.DataclassDTO(asgi_connection: ASGIConnection)[source]#
Bases:
AbstractDTO
,Generic
[T
]Support for domain modelling with dataclasses.
Create an AbstractDTOFactory type.
- Parameters:
asgi_connection – A
ASGIConnection
instance.
- classmethod generate_field_definitions(model_type: type[DataclassProtocol]) Generator[DTOFieldDefinition, None, None] [source]#
Generate
FieldDefinition
instances frommodel_type
.- Yields:
FieldDefinition
instances.
- classmethod detect_nested_field(field_definition: FieldDefinition) bool [source]#
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 app.lib.dto.SQLAlchemyDTO(asgi_connection: ASGIConnection)[source]#
Bases:
AbstractDTO
,Generic
[T
]Support for domain modelling with SQLAlchemy.
Create an AbstractDTOFactory type.
- Parameters:
asgi_connection – A
ASGIConnection
instance.
- config: ClassVar[SQLAlchemyDTOConfig]#
Config objects to define properties of the DTO.
- classmethod generate_field_definitions(model_type: type[DeclarativeBase]) Generator[DTOFieldDefinition, None, None] [source]#
Generate DTO field definitions from a SQLAlchemy model.
- Parameters:
model_type (Type[sqlalchemy.orm.DeclarativeBase]) – The SQLAlchemy model type to generate field definitions from.
- Yields:
collections.abc.Generator[litestar.dto.data_structures.DTOFieldDefinition, None, None] – A generator yielding DTO field definitions.
- Raises:
RuntimeError – If the mapper cannot be found for the model type.
NotImplementedError – If an unsupported property or extension type is encountered.
ImproperConfigurationError – If a type cannot be parsed from an element.
- classmethod detect_nested_field(field_definition: FieldDefinition) bool [source]#
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.
- app.lib.dto.config(backend: Literal['sqlalchemy'] = 'sqlalchemy', exclude: AbstractSet[str] | None = None, rename_fields: dict[str, str] | None = None, rename_strategy: RenameStrategy | None = None, max_nested_depth: int | None = None, partial: bool | None = None) SQLAlchemyDTOConfig [source]#
- app.lib.dto.config(backend: Literal['dataclass'] = 'dataclass', exclude: AbstractSet[str] | None = None, rename_fields: dict[str, str] | None = None, rename_strategy: RenameStrategy | None = None, max_nested_depth: int | None = None, partial: bool | None = None) DTOConfig
_summary_
- Returns:
Configured DTO class
- Return type:
- app.lib.dto.dto_field(mark: Literal['read-only', 'write-only', 'private'] | Mark) dict[str, DTOField] [source]#
Create a field metadata mapping.
- Parameters:
mark – A DTO mark for the field, e.g., “read-only”.
- Returns:
A dict for setting as field metadata, such as the dataclass “metadata” field key, or the SQLAlchemy “info” field.
Marking a field automates its inclusion/exclusion from DTO field definitions, depending on the DTO’s purpose.