Modern APIs with FastAPI and Python Transcripts
Chapter: Building a realistic API
Lecture: Pydantic models

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Now, one thing we can do is we can use our pydantic models to create something like this. I'm gonna write it directly here, then we'll
0:07 move it. We're gonna come over here and say there's a class called "location", and the location is going to have?
0:13 Well, you guessed it. City. Country that's required but has a default value of the US, and a state that is optional here, but it has to be a string,
0:23 I guess pretty much everything can be a string. So it seems really natural to do something like this.
0:30 Let's come down here and say that this is going to be a location which is of this type. Now, we could also put the units in there,
0:38 but to me, units and a location, these are not the same. So I'm just gonna leave it separate like this, and we've got to do a quick fix up here.
0:45 This will be "location dot" like that. Let's see what happens when we try to request it.
0:52 Yes, exactly. So it says this thing is not a valid pydantic model. So let's make it one. Base model, import that from pydantic.
1:01 Now it should work for FastAPI, except for I don't think it's gonna work. Hmm, something was missing.
1:08 Well, weird. The body was missing something. By default, These pydantic models here only come from http post of bodies. And that's not what we want.
1:19 We would like to just say give me this, like city out of here and give me the country out of the query string
1:24 and so on. So we can set it to a value called a "depends". It's kind of a blend of dependency injection and other stuff. Let's say that FastAPI does.
1:35 But if we said it the default to depends, it'll tell FastAPI to Look in the query string, Look at the URL parameters and so on.
1:42 Oh yeah, Look at that. Portland non-metric. And if we go back here and refresh it by clicking that, look at that. Portland,
1:50 Oregon, US imperial. And again, if we were to, like, set this to well taken on that just takes of the string,
1:57 there's not a good way to omit it because it's all required. But if we took away the default value here,
2:03 we don't pass a country, you can see just like before it's doing that validation say "no, no, no,
2:07 that was required". So we can set one of these pydantic models here and say
2:11 it depends, and you can even see that we could have further stuff like I could
2:15 have other, and this is, this is like some kind of recent pydantic model, Right? It has other information about it, or like nearby I don't know.
2:26 So we can actually nest these pydantic models and get that binding to flow through all of them, which is pretty awesome.
2:33 But for now, we're just gonna leave it like this. Okay, Now, where do we put this? Not here, that is. We just went through all this work to do so
2:40 much organization. Right? Well, let's go over here and make a directory called "models"
2:46 and you can bet that we're gonna have more than one by the time we're done. It's not directory. Let's open a Python file called "location" and in here,
2:55 that's good. Clean this up and we're going to get rid, import this. Alright, perfect.
3:05 Don't need that. Just make sure everything's still hanging together. Yes, Yes, it is. Perfect. Okay, so this is a
3:12 nicer way to organize things. You know, to be fair, we really only had in our models three things,
3:18 But you could easily imagine some API that gets more and more complicated. You're only getting more value by moving over to these pydantic models.


Talk Python's Mastodon Michael Kennedy's Mastodon