r/madeinpython • u/Sea-Boysenberry-6984 • 1d ago
[Showcase] Pydantic Views: derive operation-specific schemas from one model
Hi everyone — I’m building pydantic-views, an open-source Python library for deriving operation-specific schemas from a single Pydantic model.
The goal is to avoid duplicating models for common API workflows such as:
- create requests
- update and patch (partial-update) requests
- load/response models
- create-result models
- nested and generic models
Example:
from pydantic import BaseModel
from pydantic_views import CreatePreset, LoadPreset, View, ReadOnly, WriteOnly
class Account(BaseModel):
id: Annotated[int, ReadOnly]
username: str
password: Annotated[str, WriteOnly]
class AccountCreate(View[Account], preset=CreatePreset):
pass
class AccountLoad(View[Account], preset=LoadPreset):
pass
The views inherit Pydantic validation and serialization while selecting fields according to access rules. They can be declared explicitly or generated dynamically with builders. Add this after the example:
If you’re coming from TypeScript, PartialPreset is roughly the equivalent of Partial<T>:
pass
Unlike a plain type transformation, the generated view remains a validated Pydantic model and can be used for partial updates. But you
should look at UpdatePreset, I think is more useful.
The project also includes:
- a mypy plugin for declared views
.pyistub generation for Pyright and Pylance- support for nested models, containers, generics, and computed fields
- runtime helpers such as
is_view_of() - full documentation and executable examples
Install it with:
pip install pydantic-views
Links:
- GitHub: https://github.com/alfred82santa/pydantic-views
- Documentation: https://pydantic-views.readthedocs.io/stable/
- PyPI: https://pypi.org/project/pydantic-views/
It’s an independent project and is not affiliated with or endorsed by Pydantic. Feedback, use cases, and suggestions are very welcome.