Comparison of FastAPI and Django

Comparison of FastAPI and Django

This is an unfinished product.

For most of 2022, I've been working on an app that I built from scratch in FastAPI. This was the first serious project I've built that is actually in production right now. I chose FastAPI because it seemed like a newer version of Flask.

Through this exercise, I learned the value of different abstractions that previously seemed like bloat. A side effect of building in FastAPI was that some basic things like sessions, which are neatly abstracted away in web frameworks needed to built from scratch. This enabled me to get a very deep understanding of how things work under the hood.

To give you an idea of how unsophisticated I was in building this app, I didn't use an Object-Relational Mapper (ORM). Everything was written in SQL. On top of that, I didn't want to deal with migrations. So the data model was essentially a JSON field in a SQLite database. Since this app is intended to have users in the hundreds, this approach has worked fine. But there was a lot of technical debt that I built. So even though I'm able to make modifications to the app, it does require more time and probably I cannot handover this app to another developer without a massive knowledge sharing session.

I've since then attempted to graduate to Django. For most practical purposes, and rapid development, Django would work well. I did the initial quick start tutorial and found it easy. What I like about Django is that a lot of administrative, boilerplate stuff is neatly organized. In contrast, working in FastAPI, I have to manage these myself.

Something that I'd like to do is be able to bring the organization of Django into building a tool to build FastAPI applications quickly.

Django has the concept of a project that is the first thing you initialize. This is similar to the concept of FastAPI app.

Within a Django project, you create apps. In FastAPI, these would be APIRouters that will be linked to the app.

In FastAPI, you include the router in the app. In Django, you define urlpatterns in the Django app, and include a reference to that in the Django project's to include in the app.


Static files.

You can add static files by adding a directory called static within the sub-app directory. Then you can reference these files in your templates using {% load static %} in a page that uses static assets (generally base layout file} and using {% static 'path/to/static/asset' %}

One important thing that tripped me up here was that the path to the static asset is case sensitive. Logo.svg and logo.SVG are two different files. Make sure you are cognizant of that.


I hope to continue to update this post as I play more with Django, but hopefully this gives you some insight.

I'd say if you're starting out and your goal is to build a full fledged web app with users and authentication, I'd recommend to start with Django. It has a slightly steep upfront learning curve but it's worth it.

If you are building an API, then FastAPI is the way to go. If you're building a fairly simple web app with some dynamic pages, then FastAPI is still a good choice.