0% found this document useful (0 votes)
2 views3 pages

Session4 FastAPI

Uploaded by

kamandamulwa
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views3 pages

Session4 FastAPI

Uploaded by

kamandamulwa
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

SQLite - is a database that lives in a single file on your computer.

(No installation, no
configuration, no server to run)

Project : Student Grade tracker

> Teacher can add student, record their grades , view , update them and delete.
> real CRUD

grade_tracker/
|-----[Link] - FastAPI app, runs everything
|-----[Link] - SQLite connection
|-----[Link] - Student table definition
|-----[Link] - pydantic validation schemas
|-----[Link] - database operations

FILE 1- [Link] ( the foundation)


> it sets up the connection between our python code and the database

FILE 2 - [Link] (the database table)


> this is where we describe our database table.
> we write a Python class and SQLAlchemy turns it into a real table in our database (we
are not writing SQL- we are writing python)
> SQLAlchemy does SQL for us
> Talks to the DATABASE - model is what the database sees

FILE 3 - [Link] (the data validator)


> Talks to the API USER - schema is what the user sees

FILE 4 - [Link] (the database operations)


> this is where the actual database work happens (CRUD)
> we write one function for each operation

model_dump() - converts pydantic schema to plain dictionary


student.model_dump() - gives a dictionary like {‘name’ : ‘Navas’ , ‘course’ : ‘Data
Science’ , ‘grade’ : 85.5 }
> the ** unpacks that dictionary ( {‘name’ : ‘Navas’ , ‘course’ : ‘Data Science’ , ‘grade’ :
85.5 }
) and passes each key-value pair as keyword argument.
> [Link](name = ‘Navas’ , course = ‘Data Science’ ..)

> [Link]([Link])
- Start query on Student table. This is like saying (SELECT * FROM students)

> .filter([Link] == student_id)


- Add a WHERE clause. Only return the student where the id matches
- WHERE id = student_id - in SQL
> .first()
-return only the first results, or None if nothing found.
- since id is unique, there can only ever be one match

> SELECT * FROM students WHERE id = 1 LIMIT 1

> if not student ……… - if no student was found with that id , return None
> updates = data.model_dump(exclude_unset=True)
> setattr - sets an attribute on an object by name
- setattr(student, ‘grade’, 92) same as -[Link] = 92

FILE 5- [Link] (the entry point - where it all connects)


> creates the FastAPI app
> create database table
> defines all the endpoints
> db: Session = Depends(get_db) - Dependency injection. FastAPI calls get_db, gets the
session and passe it to db
> return crud.create_student(db) - Delegate to [Link].

You might also like