starlite.dto#

starlite.dto.get_field_type(model_field: ModelField) Any#

Given a model field instance, return the correct type.

Parameters:

model_field (ModelField) – pydantic.fields.ModelField

Returns:

Type of field.

class starlite.dto.DTO#

Bases: GenericModel, Generic[T]

Data Transfer Object.

classmethod from_model_instance(model_instance: T) DTO#

Given an instance of the source model, create an instance of the given DTO subclass.

Parameters:

model_instance (T) – instance of source model.

Returns:

Instance of the DTO subclass.

async classmethod from_model_instance_async(model_instance: T) DTO#

Given an instance of the source model, create an instance of the given DTO subclass asynchronously.

Parameters:

model_instance (T) – instance of source model.

Returns:

Instance of the DTO subclass.

to_model_instance() T#

Convert the DTO instance into an instance of the original class from which the DTO was created.

Returns:

Instance of source model type.

class starlite.dto.DTOFactory#

Bases: object

Create DTO type.

Pydantic models, TypedDict and dataclasses are natively supported. Other types supported via plugins.

__init__(plugins: Optional[List[PluginProtocol]] = None) None#

Initialize DTOFactory

Parameters:

plugins – Plugins used to support DTO construction from arbitrary types.

__call__(name: str, source: ~typing.Type[~starlite.dto.T], exclude: ~typing.Optional[~typing.List[str]] = None, field_mapping: ~typing.Optional[~typing.Dict[str, ~typing.Union[str, ~typing.Tuple[str, ~typing.Any]]]] = None, field_definitions: ~typing.Optional[~typing.Dict[str, ~typing.Tuple[~typing.Any, ~typing.Any]]] = None, base: ~typing.Type[~starlite.dto.DTO] = <class 'starlite.dto.DTO'>) Type[DTO]#

Given a supported model class - either pydantic, TypedDict, dataclass or a class supported via plugins, create a DTO pydantic model class.

An instance of the factory must first be created, passing any plugins to it. It can then be used to create a DTO by calling the instance like a function. Additionally, it can exclude (drop) attributes specifies in the ‘exclude’ list and remap field names and/or field types.

For example, given a pydantic model

MyClassDTO is now equal to this:

It can be used as a regular pydantic model:

This will affect parsing, validation and how OpenAPI schema is generated exactly like when using a pydantic model.

Note: Although the value generated is a pydantic factory, because it is being generated programmatically, it’s currently not possible to extend editor auto-complete for the DTO properties - it will be typed as a Pydantic BaseModel, but no attributes will be inferred in the editor.

Parameters:
  • name (str) – This becomes the name of the generated pydantic model.

  • source (type[T]) – A type that is either a subclass of BaseModel, TypedDict, a dataclass or any other type with a plugin registered.

  • exclude (list[str] | None) – Names of attributes on source. Named Attributes will not have a field generated on the resultant pydantic model.

  • field_mapping (dict[str, str | tuple[str, Any]] | None) – Keys are names of attributes on source. Values are either a str to rename an attribute, or tuple (str, Any) to remap both name and type of the attribute.

  • field_definitions (dict[str, tuple[Any, Any]] | None) – Add fields to the model that don’t exist on source. These are passed as kwargs to pydantic.create_model().

Returns:

Type[DTO[T]]

Raises:

ImproperlyConfiguredException – If source is not a pydantic model, TypedDict or dataclass, and there is no plugin registered for its type.