API Reference

Field annotations

pydantic_views.ReadAndWrite: TypeAlias

Read and write field annotation. Field could be read and written always.

alias of Annotated[T, READ_AND_WRITE]

pydantic_views.ReadOnly: TypeAlias

Read only field annotation. Field could be read always but never written.

alias of Annotated[T, READ_ONLY]

pydantic_views.WriteOnly: TypeAlias

Write only field annotation. Field could be written always but never read.

alias of Annotated[T, WRITE_ONLY]

pydantic_views.ReadOnlyOnCreation: TypeAlias

Read only on creation field annotation. Field could be read only after creation, and never again.

alias of Annotated[T, READ_ONLY_ON_CREATION]

pydantic_views.WriteOnlyOnCreation: TypeAlias

Write only on creation field annotation. Field could be written only after creation, and never again.

alias of Annotated[T, WRITE_ONLY_ON_CREATION]

pydantic_views.Hidden: TypeAlias

Hidden field annotation. Field could not be read or written.

alias of Annotated[T, HIDDEN]

Classes

class pydantic_views.AccessMode(*values)

Bases: Enum

Field access mode.

READ_AND_WRITE = 1

Read and write mark.

READ_ONLY = 2

Read only mark.

WRITE_ONLY = 3

Write only mark.

READ_ONLY_ON_CREATION = 4

Read only on creation mark.

WRITE_ONLY_ON_CREATION = 5

Write only on creation mark.

HIDDEN = 6

Hidden mark.

class pydantic_views.Builder(
view_name: str,
access_modes: tuple[AccessMode, ...],
all_optional: bool = False,
all_nullable: bool = False,
hide_default_null: bool = False,
include_computed_fields: bool = False,
)

Bases: object

View builder. It create a view classes from models following given criteria.

Parameters:
  • view_name – View name.

  • access_modes – Access modes to filter for.

  • all_optional – Make all fields optionals. On updates it allows to send just fields you want to change.

  • all_nullable – Make all fields nullable. On some kinds of updates it could meant set default value.

  • hide_default_null – Hide None as default value. It produces better examples.

  • include_computed_fields – Whether computed fields must be included on view or not.

build_view(
model: type[T],
) type[View[TypeVar] | T]

Builds a view from model if it does not exist, otherwise it return already created one.

Parameters:

model – Model class.

Returns:

View of model class.

get_view_ref()

Returns a view of model or a forward reference to it.

Parameters:

model (type[T]) – Model class.

Returns:

View of model class or reference to it.

Return type:

type[View[T] | T] | ForwardRef

build_from_model(
model: type[T],
) type[View[TypeVar] | T]

Builds a view from model

Parameters:

model – Model class.

Returns:

View of model class.

pydantic_views.BuilderCreate(view_name: str = 'Create') Builder

Default builder for Create view. Views created by it keep fields with access mode READ_AND_WRITE, WRITE_ONLY and WRITE_ONLY_ON_CREATION. And hide default None value. It produces a better schema examples.

Parameters:

view_name – View name.

Returns:

Builder configured for Create views.

pydantic_views.BuilderCreateResult(
view_name: str = 'CreateResult',
) Builder

Default builder for CreateResult view. Views created by it keep fields with access mode READ_AND_WRITE, READ_ONLY and READ_ONLY_ON_CREATION. And includes computed fields.

Parameters:

view_name – View name.

Returns:

Builder configured for CreateResult views.

pydantic_views.BuilderUpdate(view_name: str = 'Update') Builder

Default builder for Update view. Views created by it keep fields with access mode READ_AND_WRITE and WRITE_ONLY. And make all fields optional.

Parameters:

view_name – View name.

Returns:

Builder configured for Update views.

pydantic_views.BuilderLoad(view_name: str = 'Load') Builder

Default builder for Load view. Views created by it keep fields with access mode READ_AND_WRITE and READ_ONLY. And includes computed fields.

Parameters:

view_name – View name.

Returns:

Builder configured for Load views.

pydantic_views.ensure_model_views(model: type[T])

Ensures model has a view manager and returns it.

Parameters:

model – Model class.

Returns:

Views manager for model class.

class pydantic_views.Manager(model: type[TModel])

Bases: Generic

Views manager for a given model class.

property model: type[TModel]

Associated model class.

Returns:

Model class associated.

build_view(builder: Builder) type[View[TModel] | TModel]

Build view class for Manager’s model.

Parameters:

builder – Builder to use to make the view of model.

Returns:

View of model class.

class pydantic_views.View

Bases: BaseModel, Generic

View of model.

Create a new model by parsing and validating input data from keyword arguments.

Raises [ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.

self is explicitly positional-only to allow self as a field name.

model_config = {'protected_namespaces': ('view_class_root', 'view_build_to', 'view_apply_to', 'view_build_from')}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

classmethod view_class_root() type[T]

Returns the class object associated to the view.

Returns:

Associated model class.

classmethod view_build_from(model: T)

Build view from given model.

Parameters:

model – Model class to build view from.

Returns:

View of model class.

view_build_to() T

Build associated model from view data.

Returns:

Associated model instance with view data.

view_apply_to(model: T) T

Apply view data to associated model.

Parameters:

model – Model instance to use as base..

Returns:

Associated model instance with view data merged to given model data.

class pydantic_views.RootView(root: RootModelRootType = PydanticUndefined)

Bases: View[RootModel[TypeVar]], RootModel[TypeVar], Generic

View for root models.

Create a new model by parsing and validating input data from keyword arguments.

Raises [ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.

self is explicitly positional-only to allow self as a field name.

model_config = {'protected_namespaces': ('view_class_root', 'view_build_to', 'view_apply_to', 'view_build_from')}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].