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