A library to generate Pydantic models from SQLAlchemy models.
Project description
🐍🔗 SQLAlchemy-Pydantic Codegen
A Python library for generating Pydantic models from SQLAlchemy models, providing a seamless integration between SQLAlchemy and Pydantic for data validation and serialization.
✨ Key Features
- Automatic Pydantic model generation from SQLAlchemy models.
- Relationship support: Nested models for SQLAlchemy relationships.
- Custom JSON/JSONB field mapping to your own Pydantic models.
- Auto-generated
__init__.pyfor schema packages.
📦 Installation
uv add sqlalchemy-pydantic-codegen
🚀 Usage
We recommend sqlacodegen to generate your SQLAlchemy models automatically.
Once your SQLAlchemy models are ready, generate Pydantic models with:
sqlalchemy-pydantic-codegen --models-path my_app.db.models --output-dir src/schemas
--models-path: Dotted path to your SQLAlchemy models (required)--output-dir: Output directory for generated schemas (default:src/schemas)
🛠️ Custom Configuration
To map JSON/JSONB fields to custom Pydantic models, use the --config option.
Create a config file (e.g., codegen_config.py):
# codegen_config.py
# Maps table names to a dictionary of field names and the Pydantic model to use.
CUSTOM_JSONB_MODELS = {
"my_table": {
"my_jsonb_field": "MyCustomPydanticModelForJsonbField",
},
}
# Maps the Pydantic model name to its full import statement.
CUSTOM_IMPORTS = {
"MyCustomPydanticModelForJsonbField": "from my_app.schemas import MyCustomPydanticModelForJsonbField",
}
Then, run the command with the --config flag:
sqlalchemy-pydantic-codegen --models-path my_app.db.models --output-dir src/schemas --config codegen_config.py
📤 Output
- One Pydantic schema file per SQLAlchemy model.
- init.py with all exports and forward references.
- Cleaned and ready-to-use Pydantic models.
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file sqlalchemy_pydantic_codegen-1.0.2.tar.gz.
File metadata
- Download URL: sqlalchemy_pydantic_codegen-1.0.2.tar.gz
- Upload date:
- Size: 14.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.15
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2b56bac31fdcc0f9b814a8e85a0d6f656fdbfa6bdfa51479ff23ccfecfc5e98a
|
|
| MD5 |
1b2e3ce176adbf9887ed70a298221364
|
|
| BLAKE2b-256 |
239222827bcbad717309eebda97b12d9476a64a46202c6724f89112257331204
|
File details
Details for the file sqlalchemy_pydantic_codegen-1.0.2-py3-none-any.whl.
File metadata
- Download URL: sqlalchemy_pydantic_codegen-1.0.2-py3-none-any.whl
- Upload date:
- Size: 13.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.15
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1e9b3c00ca38da1e0f34883ae91ea844663000c68fc82aaba75331dd9eaa8c61
|
|
| MD5 |
c4aceb26cc299d1fee1289315ba1d500
|
|
| BLAKE2b-256 |
61795656f6dabfe7089e5a80885d0284466e6b36e75153ea8ea5a14aa7b82c05
|