dto
Construct Pydantic models from SQLAlchemy ORM types.
DTOConfig
dataclass
¶
DTOField
dataclass
¶
For configuring DTO behavior on SQLAlchemy model fields.
pydantic_field
class-attribute
¶
If provided, used for the pydantic model for this attribute.
pydantic_type
class-attribute
¶
Override the field type on the pydantic model for this attribute.
validators
class-attribute
¶
Single argument callables that are defined on the DTO as validators for the field.
FromMapped ¶
Bases: BaseModel
, Generic[AnyDeclarative]
Produce an SQLAlchemy instance with values from a pydantic model.
Config ¶
Set orm_mode for to_mapped()
method.
__class_getitem__ ¶
Decorate cls
with result from factory()
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
item |
Annotated[type[AnyDeclarative], DTOConfig | Literal['read', 'write']]
|
Can be either of a SQLAlchemy ORM instance, or a |
required |
Returns:
Type | Description |
---|---|
type[FromMapped[AnyDeclarative]]
|
A new Pydantic model type, with |
type[FromMapped[AnyDeclarative]]
|
from the SQLAlchemy model, respecting any declared configuration. |
__init_subclass__ ¶
to_mapped ¶
Create an instance of self.__sqla_model__
Fill the bound SQLAlchemy model recursively with values from this dataclass.
Mark ¶
For marking column definitions on the domain models.
Example:
Purpose ¶
For identifying the purpose of a DTO to the factory.
The factory will exclude fields marked as private or read-only on the domain model depending on the purpose of the DTO.
Example:
config ¶
Parameters:
Name | Type | Description | Default |
---|---|---|---|
purpose |
Purpose | Literal['read', 'write']
|
Is the DTO for parsing "write" data, or serializing "read" data? |
required |
exclude |
set[str] | None
|
Omit fields from dto by key name. |
None
|
Returns:
Type | Description |
---|---|
DTOConfig
|
|
field ¶
Create dto.DTOField()
wrapped in a dict for SQLAlchemy info field.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
mark |
Mark | Literal['read-only', 'private'] | None
|
How this field should be treated by the model factory. |
None
|
pydantic_type |
Any | None
|
Override the type annotation for this field. |
None
|
pydantic_field |
FieldInfo | None
|
Result of Pydantic's |
None
|
validators |
Iterable[Callable[[Any], Any]] | None
|
Added to the generated model as validators, with |
None
|