Modern APIs with FastAPI and Python Transcripts
Chapter: Course conclusion and review
Lecture: Review: Pydantic objects

Login or purchase this course to watch this video and the rest of the course contents.
0:00 The next major thing we talked about was pydantic models that are kind of like data
0:04 classes, but do all sorts of cool conversions and validation.
0:07 So imagine we want to accept some kind of order.
0:10 It has an item ID, a created date, a price and pages that are visited, and the
0:15 item ID, the created date and the price are all required.
0:18 The item ID is an int, price is a float,
0:21 date is a datetime, and then there's pages visited,
0:23 which could be a list of integers,
0:25 or it could be nothing and has a default value of just an empty list.
0:28 So if we want to describe our data like that,
0:31 we can do this very concisely by just deriving from the base model.
0:35 Then, given some dictionary, like this
0:36 ordered JSON here, where the types are convertible
0:39 too, but not exactly what we're looking for.
0:42 For example, item ID is a string,
0:44 but the number "123" as a string can be converted to 123 and the pages visited
0:49 is a list of almost numbers which could be converted to numbers and so on.
0:53 What we have to do is pass that data over using the "**" operator to
0:57 turn it into keyword arguments like item ID equals the string 123 and so
1:02 on. It will automatically convert that into a proper order or complain and give us
1:06 a meaningful error message about what was missing,
1:09 what couldn't be converted and so on.
1:10 So pydantic models are super useful on their own.
1:14 But we saw that FastAPI makes really important usage of it,
1:17 right? We can pass it as arguments to functions and a stuff submitted to the
1:21 API automatically gets converted.
1:23 We can use them for documentation by setting the response model, all that kind of stuff.
1:28 So pydantic plays an important role in FastAPI.