MongoDB with Async Python Transcripts
Chapter: Foundations: Pydantic
Lecture: Built on Pydantic
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
Let's survey the landscape of things that use Pydantic. Pydantic is awesome on its own and you can use it in even say web frameworks that have
0:10
no idea what Pydantic is and have no dependency on it. Like a Flask website that exchanges JSON, you could manually use Pydantic.
0:20
But there are many frameworks out there that are Pydantic at their core. So let's talk about a couple of those.
0:28
We have Beanie, obviously one of the centerpieces of this course, right? That's how we're modeling our data in MongoDB and doing our queries.
0:37
All of those models are based on Pydantic. So Pydantic is at the very center of working with MongoDB using Beanie.
0:47
We also have in the relational side, we're not going to use in this course for sure because MongoDB is not a RDBMS.
0:55
We have SQL model and SQL model is basically take SQLAlchemy and replace the SQLAlchemy models with Pydantic models and then that's more or less it.
1:08
But really really cool that you have these nice Pydantic models to model your data in relational databases such as Postgres or SQLite.
1:18
This is also created by Sebastian Ramirez, the creator of FastAPI. Speaking of FastAPI, FastAPI is the most well known use case for pydantic.
1:31
And it plugs right into the API data exchange in super clever ways.
1:38
Here is a pydantic class modeling an item, I guess, and that's what we're calling it as a name, a description, a price and a tax.
1:47
We're going to get into this, but the name is a string and the price is a float and they
1:52
have to be a float or parsable as a float in the data exchange for this to be valid.
1:57
If we want to say we're going to have an API endpoint, and that API endpoint accepts a JSON
2:05
post body or something like that, that has a name, description, price and tax, and we want to
2:10
automatically convert that, well, all we have to do is create a function here we have create item,
2:16
and it's just a post operation to slash items. And look in the parameter here, it just says
2:22
we take an item of type item and FastAPI will say I see what's coming in. This is the pedantic
2:28
model. And we're going to use that Pydantic model to parse and validate the JSON coming in.
2:33
So FastAPI, absolutely pedantic at the core. There's many other uses for the Pydantic models
2:39
here as well, such as response model for open API documentation, but not a class on fast
2:45
API. So we're not getting into that. But right here, specifying the type as a Pydantic model
2:52
is not just a hint to the editor, it changes how FastAPI processes the response that comes
2:58
to it. That was a bunch of awesome projects built on top of pedantic, right? Well, let's
3:06
Let's look at an awesome list to round out this small section here. Over on GitHub, we can find github.com/cludexawesome-pydantic.
3:18
It's a curated list of awesome things related to, well, Pydantic.
3:22
You can scroll through here and there's things on machine learning that are based on Pydantic. You can go down here and locate.
3:29
There's Beanie, but also Piccolo, which is an async query builder in ORM, which can auto-generate
3:34
Pydantic models from database tables, or gigantic Pydantic model support for Django or HTTP
3:43
runner, which is a HTTP testing framework, strawberry graph, and so on.
3:49
I'm not going to go through this whole list, just kind of scroll through it here. There's a bunch more awesome things to find around Pydantic.
3:56
And if you know one that's not on the list, do these folks a favor and submit a PR.