Skip to content

from_mapped

Using this implementation instead of the starlite.SQLAlchemy plugin DTO as a POC for using the SQLAlchemy model type annotations to build the pydantic model.

Also experimenting with marking columns for DTO purposes using the SQLAlchemy.Column.info field, which allows demarcation of fields that should always be private, or read-only at the model declaration layer.

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__

__class_getitem__(item)

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 typing.Annotated annotation where the first argument is a SQLAlchemy ORM instance, and the second is an instance of DTOConfig.

required

Returns:

Type Description
type[FromMapped[AnyDeclarative]]

A new Pydantic model type, with cls as its base class, and additional fields derived

type[FromMapped[AnyDeclarative]]

from the SQLAlchemy model, respecting any declared configuration.

__init_subclass__

__init_subclass__(model=None, **kwargs)

Set __sqla_model__ on type.

Parameters:

Name Type Description Default
model type[AnyDeclarative] | None

Model represented by the DTO

None
kwargs Any

Passed to super().__init_subclass__()

{}

to_mapped

to_mapped()

Create an instance of self.__sqla_model__

Fill the bound SQLAlchemy model recursively with values from this dataclass.