Why Still Use Django Over FastAPI?
Tags: python, django, fastapi, softwareengineering
Published: 2024-07-04 21:54:13

As a developer, I continue to use Django despite all the hype around FastAPI. You might be thinking, "Dude, why complicate things? Just use FastAPI and make it simple." Let's dive into this topic by discussing some use case scenarios where Django still shines.

## My Work Context

I work as a freelancer on various projects and also in a company setting. My primary role is as a mobile application developer using Dart/Flutter. This means that my work often involves creating MVPs (Minimum Viable Products) and startup projects that need to go into production as quickly as possible. This requires a software engineer to be mindful of several factors:

### Developer Time and Popularity

First, there's the cost of developer time and the popularity of the language or framework being used. Choosing a well-known and widely-used framework can save time and resources in the long run.

### Speed and Efficiency

Second, time is of the essence. As a developer, my main focus is on reducing the time spent developing and maintaining project code. This includes writing code, deploying it, migrating it, and testing it. Additionally, I need to be able to fix bugs quickly and add features as soon as possible, especially for startups. There's simply no time to be constantly migrating database schemas or worrying about the code not starting.

## The Solution: Why Django?

To address these challenges, I focus on a few key points:

### Using Python

Python is incredibly popular due to its ease of use and the robust libraries it offers. This makes it easy to get developers up to speed and working on a project quickly, especially if the project is well-documented (which, as a diligent software engineer, I ensure it is! 😊).

### Using Django

As part of the Python ecosystem, Django is a robust framework that many rely on for production-ready applications. While it does have its downsides, which we'll discuss later, it offers several benefits that make it a solid choice over FastAPI:

- **ORM (Object-Relational Mapping)**: Django's ORM simplifies database interactions, allowing for effortless migrations and providing an array of field types that cover almost every need a developer might have.
  
- **Admin Panel**: The Django admin panel is one of my favorite features. It saves a tremendous amount of time for startups and clients by providing a ready-made interface to manage application data. While a custom admin panel might be necessary down the line, the default admin panel is a great starting point for early-stage projects.

- **Extensions**: Django has a rich ecosystem of extensions that can be easily integrated, further speeding up the development process.

- **Authentication**: Built-in authentication support in Django simplifies the implementation of user management, saving time and effort.

Additionally, Django signals are a crucial feature for me. They provide a way to trigger events and execute code in response to changes in the data, eliminating the need to create a custom system for it.

## APIs for Mobile Applications

Given that I primarily develop mobile applications, APIs are a crucial part of my work. To be fair, FastAPI does offer a significant benefit with its OpenAPI generated schema. This feature greatly enhances developer productivity by providing automatically generated API documentation. Front-end developers appreciate this as it allows them to get API docs quickly and use client-side generators with the OpenAPI file/code.

### Combining Django with Django Ninja

To leverage this benefit, I use Django Ninja. While I'm not a fan of Django REST Framework (DRF) due to its performance and reliance on "magic," Django Ninja strikes a balance. It's similar to FastAPI in its simplicity and effective use of type hinting, but it doesn't overload with unnecessary features. Django Ninja also utilizes Pydantic for data validation, which adds to its robustness.

## Embracing the Magic of Django

A lot of senior backend developers tend to dislike the "magic" that Django offers. However, as an experienced software engineer, I see this magic as a good thing—especially when you understand how it works behind the scenes. By reading the Django documentation and gaining familiarity with other frameworks like Flask, FastAPI, and SQLAlchemy, you can demystify the magic. This understanding enables you to appreciate the productivity boost that Django's abstractions provide.

## The Cons of Django

While Django has many strengths, it's important to be aware of its limitations, especially when choosing it for a project. Here are some cons from my perspective, particularly in the context of building MVPs:

### Async Code

Although Django 5 has introduced support for async code, it remains a fundamentally synchronous framework. Many of its components are not optimized for asynchronous operations, which can be a limitation if your project requires extensive async functionality.

### Performance and Memory Usage

Django can be resource-intensive. A bare metal project using Django typically consumes at least 40 MB of RAM, which can be a lot compared to other frameworks. This can be a concern if you're working on a project with strict memory usage requirements.

### Real-Time Capabilities

Django is not the best choice for applications that depend heavily on real-time features like WebSockets or Server-Sent Events (SSE). For projects requiring real-time communication, Python as a whole might not be the best option. In such cases, I would prefer using Node.js or Go, depending on the specific project requirements.

# Conclusion
In conclusion, while FastAPI presents compelling advantages in API generation and performance, Django remains my preferred choice for backend development in mobile application MVP projects. Its robust ecosystem, ease of use within the Python ecosystem, and productivity-enhancing features like the admin panel outweigh its limitations in async operations and real-time capabilities. Understanding these trade-offs ensures I can effectively choose and utilize Django for projects requiring rapid development and scalability.