Defining models and their field types with Pydantic
Pydantic is a powerful library for defining data models using Python classes and type hints. This approach makes those classes completely compatible with static type checking. Besides, since there are regular Python classes, we can use inheritance and also define our very own methods to add custom logic.
In Chapter 3, Developing a RESTful API with FastAPI, you learned the basics of defining a data model with Pydantic: you have to define a class inheriting from BaseModel
and list all the fields as class properties, each one with a proper type hint to enforce their type.
In this section, we'll focus on model definition and see all the possibilities we have to define the fields.
Standard field types
We'll begin by defining fields with standard types, which only involve simple type hints. Let's review a simple model representing information about a person. You can see this in the following code sample:
chapter4_standard_field_types_01...