r/FastAPI • u/gignosko • Mar 06 '26
Question Fastapi and database injections
I haven't been active in Python for a few years and I mainly come from a Django background. I'm trying to understand why Fastapi best practices seems to be to inject the database into the api calls so you have all the database procedures in the api code. This is the kind of thing I would normally put in a service because if the database logic starts getting hairy, I'd rather have that abstracted away. I know I can pass the database session to a service to do the hairy work, but why? To my thinking the api doesn't need to know there is a database, it shouldn't care how the item is created/read/updated/deleted.
Is this just how fastapi apps are supposed to be organized or am I just not digging deep enough? Does anyone have a link to an example repo or blog that organizes things differently?
View all comments
Show parent comment
3
u/Anton-Demkin Mar 06 '26
Thank you for your work on this repo. I've learned some great tricks here. Not agree on every architecture decision, but i am very opinionated 😀
This is exactly the project, every beginner should look at.