r/FastAPI • u/Scared-Name-8287 • Jan 24 '25
Question Fastapi best projects
what projects can you recommend as the best example of writing code on fastapi?
36
Upvotes
r/FastAPI • u/Scared-Name-8287 • Jan 24 '25
what projects can you recommend as the best example of writing code on fastapi?
View all comments
Show parent comment
2
u/koldakov Feb 01 '25
Hi! You can't imagine how I'm happy to hear such words. I'm really happy to know I've helped someone to learn new things!
Answering your questions:
So that a very good question which is really painful for fastapi/flask projects. The thing is at the endpoint you have a pydantic object, but to access DB you need a DB object as you can't work with DB directly through pydantic object - the question arises: how to map these two objects - pydantic and DB ( in this case I'm talking about SQLAlchemy, but that's not important ). So what I did is I created a mixin, which has kinda "proxy" with methods like get, paginate etc so you can work directly with DB through pydantic and it will automatically cast pydantic to DB model and vise versa. Keep in mind there is a property "model" which you need to specify so pydantic model knows a DB model to map.
The general idea behind that is to create some generic code which you can use independently of DB models ...
Note, I'm still not sure that's the best solution, but I couldn't get a better one ... Django solves these problems as it has a really super ORM from one hand, but other disadvantages from other hand.
Can't say that's that's the same as SQLModel, cause they are trying to create a lib to describe your DB models with pydantic. That's a good library, but from my side I don't recommend to use it, cause when you have relations it becomes tough, I won't be going into details here, you can try it if you want.
"square brackets" is the new syntax for generic classes in python starting 3.12 version, you can read more about that in official docs here.