6 ms·
We use django for our websites where I work, and I use it for personal projects for the ORM. How do you like the ORM for FastAPI? I've wanted to try it but sin
by thewataccount 4y ago
We use django for our websites where I work, and I use it for personal projects for the ORM.
How do you like the ORM for FastAPI? I've wanted to try it but since I already know how to use django it's been hard to find a reason to try FastAPI. Currently I _really_ like Django's orm.
- Zetice 4y agoYou end up missing Django's ORM when you can't use it, as well. I've tried my best to recreate the experience with Prisma in JS but it took me awhile to get to a place where I felt like I wasn't duplicating schema...
- barefeg 4y agoFastAPI doesn’t come with an ORM out of the box. There’s https://sqlmodel.tiangolo.com/ https://sqlmodel.tiangolo.com/ which wraps SQLAlchemy and allows defining the database models with pydantic-like classes. For me neither Django nor SQLAlcjemy hit the right spot when trying to define “pure” domain models that have no dependency (I.e. no ORM information). For that I created a repository pattern approach that converts “domain mode operations” into SQLModel query operations via filter specifications (https://martinfowler.com/apsupp/spec.pdf https://martinfowler.com/apsupp/spec.pdf). The result is that I always need to define 2 models: one is my pure domain model and another is a “table model” (defined in SQLAlchemy) along with a mapping between the two (for cases where there’s no 1-to-1 trivial mapping between their fields). Also making some of the SQLAlchemy magic (for example back propagating relational attributes) is messy. But if you want to produce some quick applications I think SQLModel+FastAPI works quite well. I don’t think is as mature as Django though.
- WD-42 4y agoThere is no official ORM for FastAPI. You have to pick one yourself. Probably the most common choice is Sqlalchemy, which is not nearly as nice to work with as Django's ORM, in my opinion. It's also annoying to wire up. For non-db backed projects (but let's be honest, how many projects don't use a DB) FastAPI is a great choice.
- Bedon292 4y agoOne of the apps I worked on ended up switching away from Django because of all the magic in the ORM slowing things down significantly for us. Same DB and API, just written in FastAPI was a 4x speedup. That was 2+ years ago though, so cannot say where things currently are. And now anything new I do is FastAPI, and only deal with Django for existing apps. Yes there are annoyances with not having that ORM, but worth it for what I have been working on at least.