Loading video player...
Namaste friends, welcome back to the SI
technology. In the last video we have
discussed how to insert data in the
table using post method. Now in this
video we will learn how to get the data
from the table. So in the last video we
have just inserted only the first item
that you can see here battery. But for
the getting values I have added these
item in the database. Okay. So now we
will learn how to create the endpoint to
get the values from the database. So
here we will create one another endpoint
that is get items. Okay. So
app dot get
and here
items. Okay, don't get confused. The end
points of these are same but if we call
post method
then this will call and if we just call
using get method then this will call.
Now here what we need to provide. So the
first thing is response model. In which
model we are just responsing the values.
So here response model and at this time
we are responding a list that's why list
and of which type that is item.
We could have more than one row in the
table. Same as our example we have six
rows. So we will just provide the list
of these six. That's why we have list
here. Now define the function. So def
read
items.
Okay, read items. And we can also put
limit and escape.
So that you can just get the values
using limit and escape.
Skip
type is inta value zero. Okay. Then
limit
int default value for example 100.
Okay, the next is session. So, DB
session
and same as the previous we will just
get the session using dependency
injection. So
depends and
get db
items
equal to
crud
dot get item
get item and you can see we don't have
any get item in the
CRUD. So we need to create. So just open
the CRUD and same as we have created
create item we need to create get item.
So now define one function dev get
item and what we will get session. So
get
items and what we will get here. So DP
that is session
and the escape add limit. So
skip
and default value is zero
limit
and default value 100.
Okay.
Now return. So here we will just query
from the database. So using this DB dot
query
and what we are just putting here. So
models
dot
item.
Okay. So using this we will get all the
items. But if we want just put skip and
limit then we can just use offset
and offset is escape
and for the limit
limit and then dot all. Okay. So this
all will fetch all values after applying
offset and limit. So now the get items
is ready. Now in the main
get items and here.
Okay. So DB equal to DB
escape and limit. Now return
items. Okay. So, I am getting this error
because
I need to put DV here. Okay. So, now
let's check this.
So we have this get and here
default value is zero and limit is 100.
Now let's try it out and execute.
And here you can see the response. So
battery ball hammer refrigerator toast
and cutlery. And what we have in the
database we have from battery to
cutlery. Okay. So this was the example
how to
get the values and let's test using the
skip and limit.
So for example I am just putting skip
one and limit for example three. So it
will just skip first and show three. So
ball, hammer and refrigerator. So let's
see is it working or not.
So ball, hammer and refrigerator. Okay.
So this skip and limit is also working.
Now the next thing let's get the value
using the item ID. Okay. And for that we
need to create one another endpoint.
And here app dot get and again
items
but at this time
item id. Okay. So this is the variable.
Now
same response model
and this time we don't need to put list.
So just put the item okay because we are
getting the single item as per the ID.
Now define the function. So read
item
and here item
item id and type is int
db session
and here
depends
you get the view. Okay,
now we need to just create one another
thread function for the getting item
using id. So first let's create the cred
function.
So here for getting the single item
using item id we are going to create
function. So defaf get item
db
session and one item id
and its type is int. Okay.
Now return
b dot query
and in this query first
models dot
item. Okay. And here
we will just filter. Okay. So
filter and in this filter
models dot
item
dot id equal to
item ID. Okay. And then
first
now let me explain.
So DB dot query. So what query we are
putting item that means from the item
table we are getting the first item and
where we have some condition. So based
on condition just filter.
So what condition is model dot item do
id will be equal to item id that user is
putting here. Okay. So if you put four
in the request then from the model dot
item do ID
it will get that item which has item id
is four. Okay. So this is the simple
query that we need to put for getting
the item based on item id. Now again
come to the main. So here DB
item equal to
CRUD dot
get
item. And here DB equal to DB
item id equal to
item ID. So in some cases we don't get
any value because the value is not
available in the database then we need
to raise one HTTP exception.
So here we will just put one if
condition. So if
DB
item
is null.
Okay. So for example you are putting
give me the value which has item id is
100. So and if that item is not
available in the database then we will
get none here and if the item id is none
we will raise one http exception. So
first let's check we have imported it
not. So
here
http exception
raise
HTTP exception
and here status code 404 item not found
and detail again we can just put the
same text. So item
not
found
and if this will not execute we will
just return
db item. Okay. So now let's check this
is working or not.
So first let me check. Yes is refreshed
now. Here
you can see the third
and here we need to put item ID that is
required. So for example if I am putting
one here I should get battery. Okay,
because the first
item that is item ID is one that is
battery and if we put five, we will get
toaster. So again let's check. So put
five here
and you will get toaster. But what in
our database we don't have item ID
seven. So let's check if user will put
seven. So what happens
seven and execute and you will get the
404
exception and detail is item not found.
Okay. So this was the example how to get
the values from the database.
Now in the next video we will continue
our discussion and learn about delete
and update. So, thanks for watching this
video. See you in the next. Till then,
bye.
Welcome to another video in the FastAPI Database Tutorial series! In this video, we will learn how to use the GET method to fetch data from the database using FastAPI and SQLAlchemy. This is part of the CRUD operations where we focus on the Read functionality. We’ll create a function in our crud.py file to retrieve records from the database and call it from our API route. You will understand how to query all records or specific data based on conditions, and how to return structured results using Pydantic schemas. In this video, you’ll learn: - How to write a GET API in FastAPI. - How to fetch data from the database using SQLAlchemy queries. - How to connect CRUD operations with routes and schemas. - How FastAPI automatically converts database objects into JSON responses. By the end of this video, your FastAPI application will be able to fetch and display real data from your database efficiently and cleanly. #FastAPI #FastAPITutorial #FastAPICRUD #SQLAlchemy #FastAPIDatabase #PythonBackend #BackendDevelopment #WebDevelopment #LearnFastAPI #FastAPIProjects