🍓Unit 13: Data Types
Introduction
Besides primitive data types (int
, float
, str
, bool
), user-defined data types, FastAPI also supports advanced data types such as UUID
, datetime
. In this unit, we will delve into the use of these data types within the context of FastAPI.
You can find out all the valid Pydantic data types at https://docs.pydantic.dev/latest/concepts/types/
Extra data types
In the following table, we present additional data types from Pydantic. Because these types are provided by Pydantic, they offer all the useful features, including editor support, data conversion for incoming requests and responses, data validation, and automatic annotation and documentation.
UUID
A standard "Universally Unique Identifier", common as an ID in many databases and systems.
In requests and responses will be represented as a
str
.
datetime.datetime
A Python datetime.datetime.
In requests and responses will be represented as a
str
in ISO 8601 format, like:2024-03-20T09:41:00+07:00
.
frozenset
In requests, a list will be read, eliminating duplicates and converting it to a
set
.In responses, the
set
will be converted to alist
.The generated schema will specify that the
set
values are unique (using JSON Schema'suniqueItems
).
Example program
Let's consider the following program:
In case you don't know how to interact with the program in Postman, you can interact with its API Docs available at http://127.0.0.1:8000/docs and http://127.0.0.1:8000/redoc
Fig. 1 shows how to interact with the program in Postman.
The
generate_uuid()
function can be used to generate a dummy UUID.The value
P3D
inprocess_after
is followed the ISO 8601 format. You can find out more details at https://en.wikipedia.org/wiki/ISO_8601#Durations
Last updated