Generate Pydantic Model from SQLAlchemy Model
This package is helpful while building web apps with FastAPI and SQLAlchemy. It generates Pydantic Model from SQLAlchemy Model.
To install,
pip install sqlapydantic
You may use generate_models function directly to generate Pydantic Model from SQLAlchemy Model. It takes Generator's init arguments and init a Generator class.
Examples:
from sqlapydantic import generate_model
generate_model(models=[MyModel], base_model=CustomBaseModel)from sqlapydantic import Generator
generator = Generator(base_model=CustomBaseModel)
generator.generate_from_module(models=my_models_module, output_path="schemas.py")Generator Class takes following init arguments
split_models: Whether to split models into Base, Create, Update and Read models. Default isFakse.base_model: Base model to inherit from. Default isBaseModelfrompydantic.restrict_fields: Which takes asetof fields to restrict. Default isNone. This is useful when you want to restrict some fields to be readonly such as id, created_at, updated_at.indentation: Indentation to use in generated code.
- Strict typing, such as using
conintfor limitingIntegersize andconstrforStringlength. - Probably, generate relationships as well.