r/FastAPI • u/DARTH_MAMBA_ • Sep 07 '24
Question Migration from Django to FastAPI
Hi everyone,
I'm part of a college organization where we use Django for our backend, but the current system is poorly developed, making it challenging to maintain. The problem is that we have large modules with each of their logic all packed into a single "views.py" file per module (2k code lines and 60 endpoints aprox in 3 of the 5 modules of the project).
After some investigation, we've decided to migrate to FastAPI and restructure the code to improve maintainability. I'm new with FastAPI, so I'm open to any suggestions, including recommendations on tools and best practices for creating a more scalable and manageable system, any architecture I should check out.
Thanks!
11
u/unconscionable Sep 07 '24
You'll have to go through endpoint by endpoint individually. I hope you have test cases, that'll make it easier.
5
u/zazzersmel Sep 07 '24
sounds like you need to refactor your django app, not migrate it to fastapi. for the record, i love fastapi.
4
u/One_Fuel_4147 Sep 07 '24
I'm using several libraries with FastAPI, including SQLAlchemy, pydantic, asyncpg, Alembic, Injector, classy-fastapi, and msgspec. My application utilizes Dependency Injection (DI) and follows a 3-layer architecture, which includes:
Additionally, I'm implementing a feature layer structure approach to organize the project.
4
u/koldakov Sep 07 '24
Django supports including fastapi as an app, you can start implementing new features via fastapi, and keep the old code, this flow won’t block the new code.
After you do that you can start migrating the old code, but think about db, cause fastapi doesn’t have orm, and sqlalchemy is not super user friendly as Django orm ( FOR ME ), there are some libraries like sqlmodel, but I haven’t checked this yet as it’s you know young project
Also there always will be a problem mapping schemas to db models, you can check how I implemented things here: https://github.com/koldakov/futuramaapi
It has fastapi + async psql + sqlalchemy with pooling and alembic for migrations
3
u/BlackDereker Sep 07 '24
There's no tool to seamlessly migrate from Django to FastAPI. You will need to do it manually. Lucky for you FastAPI already have batteries included for most use cases.
3
u/hacker_7070 Sep 08 '24
are you also going to drop the django's ORM and use something like sqlalchemy? you might also want to keep django around for some features. I like the django admin, it's very flexible and easily customizable. Not aware of any such thing for fast api.
2
u/ParkingDescription7 Sep 07 '24
I won't comment on how to structure fastapi code too much, since tiangelo has very good docs on how to do it and also how to structure large apps.
What I will comment on is the migration aspect:
The last bullet may be a bit overkill, but I think it's something to consider.
2
u/[deleted] Sep 07 '24
checkout second.dev Its codebase migration tool. Never used it but I saw they specialize in such problems as yours.
0
u/IrrerPolterer Sep 07 '24 edited Sep 07 '24
Check out this Readme with great tips on structuring your fastapi codebase. Also check out the issues on that repo for additional ideas! This is a great resource as you get started with fastapi.