FastAPI Python Tutorial - 5: Query users based on criteria: Query parameter

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome back to the part 5 tutorial fast api in python in this video we'll learn how to add query parameters to query based on criteria from the database let's get started hi everyone i'm leanne welcome to just into data where data science and data engineer materials are shared and made simpler for you let's continue to build upon the code from the previous videos will work based on this basic get operation we'll leave the path as it is so still slash users let's change the function name to get users query since we'll add a query parameter then within the parenthesis we'll add a parameter called limit with type declared as int for integer so this function parameter is not a path parameter since we did not specify it in the past like this username so this limit is a query parameter so now i have one query parameter called limit again this is because fast api considers a function parameter as a query parameter when it is not a path parameter what can this core parameter do we'll still ask the function to graph the values of user database as user list then within the return statement instead of returning all of user lists we can add square brackets to filter 4 say from beginning colon until limit the query parameter so this curve parameter limit is used as the limit for printing only the first few users from the database that's why we've declared its type as integer since there should be an integer there so suppose we set limit to be 2 and then there's a get request sent to the pass of users only the first two users within the database will be returned in fact you probably have seen and used query parameters when surfing the internet they also appear in the urls for example this query parameter of limit can show up like this it is after the question mark in the url as key value pairs this is saying the query parameter limit is assigned a value of 2. if you're still a little confused of the definition of query parameter let's see a real live example as well so this is reddit.com within reddit there's a search box let's type in fast api you can see that there was probably a get request sent to the path of slash search with query parameter this parameter after the question mark q set as the value of fast api now if we also click sort and let's choose relevance if you look at a url again after the question mark there are two query parameters q with the value of fast api and sort with the value of relevance all right let's get back to pycharm now we have this new get operation with the current parameter limit i still have the app running within the terminal so i can just save the code and then the app should be reloaded let's go to the browser there it is the get operation to the path of users with function called get users query let's expand and try it out this time enter parameters you can see our query parameter called limit it is required as integer let's put a value of 2 and execute you can see that the url has paths of slash users followed by a question mark and query parameter limit equals 2. and we've got a response of 200 success response body has this list of dictionaries due to the query parameter limit it is showing only the first two users jack and jill but not jane we can also try to change limit to 1 and execute again so now there's only the first user of jack being returned this is great now what if we don't want to have to put a value limit each time we want to make it optional rather than required let's get back to the code again after declaring limit as integer we can also add a default value say equals 2. now let's save this code and go back to the doc we'll refresh the docs you can see that the limit query parameter is still integer but it has pre-filled the value to be 2 for us and the little red print saying is required also disappeared so we don't have to enter anything just click execute since by default the limit is 2 so only 2 users are returned this is great we can even go back to the limit parameter and remove the default value of 2 here if we execute you can see that only the first two users are returned again so fast api use the default value of query parameter in the background and if you look at the request url this is not even showing the query parameter limit since query parameter is not a fixed part of the pass that's why we can set them to be optional with the default value and fast api will still use its default value let's go back to the code and look at this get operation we built previously different from query parameters a path parameter will be a fixed part of the path so it will always be required so let's say even if we put a default value of username say equals jack now we think we've set a default value of the path parameter but if i save this code and go back to the docs refresh the doc and if we expand the other get user path operation the path parameter username is still required so to recap well we can make query parameters to be optional with default values the path parameters will always be required all right you've learned how to add query parameters that are either required or optional with default that closes the basics of retrieving data using fast api in the next video we'll learn how to write operations to add new data in the api we'll use a different operation the post operation stay tuned did you learn something new in this video if so make sure to subscribe to our youtube channel just click the subscribe button below this video right now if you're interested in more data science tutorials and courses please head over to our website justintodata.com thank you and see you in the next video
Info
Channel: Lianne and Justin
Views: 2,953
Rating: undefined out of 5
Keywords: fastapi, python, python tutorial, fastapi python, fastapi tutorial, pydantic, api python, http, http operations, http methods, get request, json, swagger, swagger ui, type hints
Id: hEng8BjkR6U
Channel Id: undefined
Length: 7min 55sec (475 seconds)
Published: Wed Jan 19 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.