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]
Classes¶
Public package surface for pydantic-views.
This module re-exports the main classes, builders, and annotations so users can import
from pydantic_views directly.
- class pydantic_views.AccessMode(*values)¶
Bases:
EnumAccess rules that determine if and when a field is exposed in generated views.
- 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:
objectFactory for generating view classes from Pydantic models based on access rules.
- Parameters:
view_name – Name suffix for the generated view class.
access_modes – Access modes that the builder will include in generated views.
all_optional – Make all fields optional (useful for update scenarios).
all_nullable – Make all fields nullable when allowed.
hide_default_null – Replace default
NonewithPydanticUndefinedto hidenullin schemas.include_computed_fields – Whether computed fields should be included in generated views.
- build_view(
- model: type[T],
Build or return a cached view for the given model.
- Parameters:
model – Model class to derive the view from.
- Returns:
View class associated to
modelfor this builder.
- get_view_ref(
- model: type[T],
Return the view class or a forward reference for the model.
- Parameters:
model – Model class to derive the view from.
- Returns:
View class or forward reference for
model.
- pydantic_views.BuilderCreate(view_name: str = 'Create') Builder¶
Default builder for
Createviews.Keeps fields with
access modeREAD_AND_WRITE,WRITE_ONLY, andWRITE_ONLY_ON_CREATION, hiding defaultNonevalues.- Parameters:
view_name – View name.
- Returns:
Builder configured for
Createviews.
- pydantic_views.BuilderCreateResult(
- view_name: str = 'CreateResult',
Default builder for
CreateResultviews.Keeps fields with
access modeREAD_AND_WRITE,READ_ONLY, andREAD_ONLY_ON_CREATION, and includes computed fields.- Parameters:
view_name – View name.
- Returns:
Builder configured for
CreateResultviews.
- pydantic_views.BuilderUpdate(view_name: str = 'Update') Builder¶
Default builder for
Updateviews.Keeps fields with
access modeREAD_AND_WRITEandWRITE_ONLY, and makes all fields optional.- Parameters:
view_name – View name.
- Returns:
Builder configured for
Updateviews.
- pydantic_views.BuilderLoad(view_name: str = 'Load') Builder¶
Default builder for
Loadviews.Keeps fields with
access modeREAD_AND_WRITEandREAD_ONLY, and includes computed fields.- Parameters:
view_name – View name.
- Returns:
Builder configured for
Loadviews.
- 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 the model class.
- class pydantic_views.Manager(model: type[TModel])¶
Bases:
GenericRegistry and factory for views derived from a model class.
Initialize a manager for the given model type.
- Parameters:
model – Base model class that views will be derived from.
- class pydantic_views.View¶
-
Lightweight view over a base Pydantic model with helper builders and mergers.
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]¶
Return the base model class this view was generated from.
- Returns:
Associated base model class.
- classmethod view_build_from(model: T)¶
Create a view instance from a model instance, omitting unset fields.
- Parameters:
model – Model instance to build the view from.
- Returns:
View populated with the model data.
- view_build_to() T¶
Build the associated model instance using only fields set on the view.
- Returns:
Model instance created from the view data.
- view_apply_to(model: T) T¶
Merge the view data into an existing model instance, returning a copy.
- Parameters:
model – Model instance used as the base.
- Returns:
New model instance with the view data applied.
- class pydantic_views.RootView(root: RootModelRootType = PydanticUndefined)¶
Bases:
View[RootModel[TypeVar]],RootModel[TypeVar],GenericView wrapper specialized for
RootModelinstances.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].