Part 5: Pydantic data models
FastAPI framework with pydantic, helps in validating data, defining data models and pass it to a response model for output.
In this blog, I will explain concepts and show you how to create a nested model.
Some of the key aspects of the data model parameters include:
- Data types
- Optional and Required fields
- Connection between `Response model` and `Model Parameters`
How does a model parameters works ?
First, you need to create a dedicated file `models.py` and use that file to define the model parameters. Creating a model is as simple as creating a class function with attributes/parameters.
In the class function, you will define the model name, database connection, table name, and attributes with data type.
In the above, class function Employee
takes in database from Base
and looks for data table Employee Table
.
Once the model class detects data table Employee Table
it will bring in the data attributes and applies parameters such as primary key, optional / mandatory, data types. etc to generate an output.
- Data Types:
To define a data type, you can just give a column parameter with the data type name. The most commonly used data types are Integer, String, and Float.
Employee_Number: int
Employee_Name: str
Employee_Number: float
2. Optional and Required fields
To make an attribute value, Optional
— you would need to declare data attribute with| None
or =Query(default=None)
Employee_Number: str | None = None
If | None
is declared, the attribute will be considered a Required
Field
Employee_Number: str
3. Connection between Response Model
and Model Parameters
Model Parameter
interacts with database and brings in data Table. This data table will be passed on to to Response Model
in main.py file.
In the below code, Employee
is the Response Model
.
Nested Models:
Nesting in outputs is quite common when structuring json files.
In the above, you are looking at three data models: 1. Employee 2. Performance and 3. Location. To connect these these three models together, you need to nest the models.
For example below:
The Group attribute in thePerformance
data model takes values from the Employee
data model and shows the results as a list.
Group: list[Employee] | None = None
When you make a GET
call with above nested models will result in the output below
Conclusion:
Creating nested models is critical to building effective APIs. In real-world scenarios, APIs carry large amounts of data to applications. Nesting is a key tool in your kit develop a powerful API.
In my upcoming posts, I will detail basic and advanced concepts. The full list of topics, both past and upcoming, is below. Thank you for reading !!
Part 1: Getting started with FastAPI
Part 2: Concepts needed to build routes
Part 3: Creating GET, POST, PUT, DELETE requests
Part 4: Middleware & Azure application Insights
Part 6: Define schemas
Part 7: Connect with SQL server to pull real-time data
Part 8: Secure your FastAPI & adding authentication
Part 9: Deploy using docker & AKS
Part 10: Bringing it all together