Skip to main content

Views for Pydantic models

Project description

Documentation Status PyPI - Python Version PyPI - Types PyPI - License PyPI - Version

View for Pydantic models documentation

This package provides a simple way to create views from pydantic models. A view is another pydantic models with some of field of original model. So, for example, read only fields does not appears on Create or Update views.

As rest service definition you could do:

ExampleModelCreate = BuilderCreate().build_view(ExampleModel)
ExampleModelCreateResult = BuilderCreateResult().build_view(ExampleModel)
ExampleModelLoad = BuilderLoad().build_view(ExampleModel)
ExampleModelUpdate = BuilderUpdate().build_view(ExampleModel)

def create(input: ExampleModelCreate) -> ExampleModelCreateResult: ...
def load(model_id: str) -> ExampleModelLoad: ...
def update(model_id: str, input: ExampleModelUpdate) -> ExampleModelLoad: ...

Features

  • Unlimited views per model.

  • Create view for referenced inner models.

  • It is possible to set a view manually.

  • Tested code.

  • Full typed.

  • Opensource.

Installation

Using pip:

pip install pydantic-views

Using poetry:

poetry add pydantic-views

How to use

When you define a pydantic model you must mark the access model for each field. It means you should use our annotations to define field typing.

from typing import Annotated
from pydantic import BaseModel, gt
from pydantic_views import ReadOnly, ReadOnlyOnCreation, Hidden, AccessMode

class ExampleModel(BaseModel):

    # No marked fields are treated like ReadAndWrite fields.
    field_str: str

    # Read only fields are removed on view for create and update views.
    field_read_only_str: ReadOnly[str]

    # Read only on creation fields are removed on view for create, update and load views.
    # But it is shown on create result view.
    field_api_secret: ReadOnlyOnCreation[str]

    # It is possible to set more than one access mode and to use annotation standard pattern.
    field_int: Annotated[int, AccessMode.READ_ONLY, AccessMode.WRITE_ONLY_ON_CREATION, gt(5)]

    # Hidden field do not appears in any view.
    field_hidden_int: Hidden[int]

    # Computed fields only appears on reading views.
    @computed_field
    def field_computed_field(self) -> int:
        return self.field_hidden_int * 5

So, in order to build a Load view it is so simple:

from pydantic_views import BuilderLoad

ExampleModelLoad = BuilderLoad().build_view(ExampleModel)

It is equivalent to:

from pydantic import gt
from pydantic_views import View

class ExampleModelLoad(View[ExampleModel]):
    field_str: str
    field_int: Annotated[int, gt(5)]
    field_computed_field: int

In same way to build a Update view you must do:

from pydantic_views import BuilderUpdate

ExampleModelUpdate = BuilderUpdate().build_view(ExampleModel)

It is equivalent to:

from pydantic import PydanticUndefined
from pydantic_views import View

class ExampleModelUpdate(View[ExampleModel]):
    field_str: str = Field(default_factory=lambda: PydanticUndefined)

As you can see, on Update view all fields has a default factory returning PydanticUndefined in order to make them optionals. And when an update view is applied to a given model, the fields that are not set (use default value) will not be applied to the model.

original_model = ExampleModel(
    field_str="anything"
    field_read_only_str="anything"
    field_api_secret="anything"
    field_int=10
    field_hidden_int=33
)

update = ExampleModelUpdate(field_str="new_data")

updated_model = update.view_apply_to(original_model)

assert isinstance(updated_model, ExampleModel)
assert updated_model.field_str == "new_data"

But if a field is not set on update view, the original value is kept.

original_model = ExampleModel(
    field_str="anything"
    field_read_only_str="anything"
    field_api_secret="anything"
    field_int=10
    field_hidden_int=33
)

update = ExampleModelUpdate()

updated_model = update.view_apply_to(original_model)

assert isinstance(updated_model, ExampleModel)
assert updated_model.field_str == "anything"

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pydantic_views-0.2.0b0.tar.gz (9.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pydantic_views-0.2.0b0-py3-none-any.whl (9.8 kB view details)

Uploaded Python 3

File details

Details for the file pydantic_views-0.2.0b0.tar.gz.

File metadata

  • Download URL: pydantic_views-0.2.0b0.tar.gz
  • Upload date:
  • Size: 9.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.0.0 CPython/3.12.3 Linux/6.8.0-1021-azure

File hashes

Hashes for pydantic_views-0.2.0b0.tar.gz
Algorithm Hash digest
SHA256 bf4e15f842831e6ec01c1209ddd15e3bc7b89be38f56c0d5dff76158d4280c8a
MD5 f73a11c8903fc69bbcf18fecd580051a
BLAKE2b-256 9bd440ccbdb8864ebec74763bddd685ee2997c62b525a330b2b850661616c6c7

See more details on using hashes here.

File details

Details for the file pydantic_views-0.2.0b0-py3-none-any.whl.

File metadata

  • Download URL: pydantic_views-0.2.0b0-py3-none-any.whl
  • Upload date:
  • Size: 9.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.0.0 CPython/3.12.3 Linux/6.8.0-1021-azure

File hashes

Hashes for pydantic_views-0.2.0b0-py3-none-any.whl
Algorithm Hash digest
SHA256 ee22b5fe0b85d3803183ee0a157b53eb378bf59f4a739c1df12a40409cc60f74
MD5 5613a070a3bec6f3fd4cc977e914f2c2
BLAKE2b-256 0ef4d230943766c971c05ad72e9c1acf8405ae56aba72b8e20fdca39d5c4521f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page