Pydantic with Flask
Posted on
by Kevin FoongI haven't seen many articles on the internet describing how to use Pydantic together with Flask so I thought of providing an overview of what I did to get this to work. For those who don't know, Pydantic is a Python library used to parse and validate data. It is typically used to ensure that data sent to your API enpoints is in the correct format and type.
- First we install a handy library Flask-Pydantic. This library is used to integrate Pydantic with Flask.
pip install Flask-Pydantic
- Next we create Pydantic schema models. These models define the required fields for the endpoint. A model is just a class that inherits from Pydantic's
BaseModel
. I normally have all models in a separatemodels_schema.py
file. - Below is an excerpt from my
models_schema.py
file.
Some notes,Read more ...