semvec/pypi-packages huggingface dataset:
flask-pydantic-serialize 0.0.2
This package provides a utility to serialize db object using pydantic models.
License: MIT
Requires Python: >=3.6
Keywords: sqlalchemy,alchemy,mysql,postgres,flask,mssql,sql,sqlite,serialize,pydantic,orm,serialization,performance,database,relational

# 🅕🅛🅐🅢🅚-🅟🅨🅓🅐🅝🅣🅘🅒-🅢🅔🅡🅘🅐🅛🅘🅩🅔

*Flask-Pydantic-Serialize* is a Python package that serializes the database objects using the pydantic models. 
This comes handy if you are pydantic along with Flask, Tornado, Pyramid or any other framework.

A single serialize call will take of validating the returned response as well as serializing it. There are options to include or exclude certain fields or exclude/include fields with null values.

[![PyPI](https://img.shields.io/pypi/v/flask-pydantic-serialize?color=g)](https://pypi.org/project/flask-pydantic-serialize/)
[![Python package](https://github.com/vivekkeshore/flask-pydantic-serialize/actions/workflows/python-package.yml/badge.svg?branch=main)](https://github.com/vivekkeshore/flask-pydantic-serialize/actions/workflows/python-package.yml)
![LGTM Grade](https://img.shields.io/lgtm/grade/python/github/vivekkeshore/flask-pydantic-serialize)
[![Coverage Status](https://coveralls.io/repos/github/vivekkeshore/flask-pydantic-serialize/badge.svg?branch=main)](https://coveralls.io/github/vivekkeshore/flask-pydantic-serialize?branch=main)
[![GitHub license](https://img.shields.io/github/license/vivekkeshore/flask-pydantic-serialize)](https://github.com/vivekkeshore/flask-pydantic-serialize/blob/main/LICENSE)


----

### Compatibility


This package is compatible with Python >= 3.6

### Basic Usage


Install with pip:

```bash
    pip install flask-pydantic-serialize
```


```python
    from sqlalchemy import create_engine
    from sqlalchemy.orm import Session


    db_engine = create_engine(DB_CONNECT_STRING)  # DB connection string, ex "sqlite:///my_app.db"
    db = Session(db_engine)
```

```python
    from pydantic_serialize import serialize    
    from typing import Optional
    from pydantic import BaseModel
    
    class UserResponseModel(BaseModel):  # Define the pydantic model for serialization.
        username: str
        age: Optional[int] = None
        phone: Optional[str] = None
        
    users = db.query(User).all()  # Multiple records. User is an example db model. Replace User with your db model.    
    res = serialize(users, UserResponseModel, many=True)  # Pass the db records, pydantic model. Set many as True if there are multiple records.
    
    user = db.query(User).first()  # Single record. User is an example db model. Replace User with your db model.
    res = serialize(user, UserResponseModel)  # Pass the db record, pydantic model. Many is set to False by default.
```

Tests
-----

Run tests:

```bash
    pytest
```


License
-------

Flask-Pydantic-Alchemy is released under the MIT License. See the bundled [`LICENSE`](https://github.com/vivekkeshore/flask-pydantic-serialize/blob/main/LICENSE) file
for details.