In the previous article, we’ve outlined the main differences between Flask and FastAPI, and we’ll continue the comparison of FastAPI - but this time, we’ll focus on Django Rest Framework (DRF) to see which one is better. Of course, the choice is never easy, and it’s hard to say that one framework is ALWAYS better than the other.
That is why we’ll write down the strengths and weaknesses so you can make an informed decision.
Also, remember that at Sunscrapers, are always happy to help you with the decision and further development - given that we specialize in Python and have over 11 years of experience in almost every framework.
What is DRF
Look at DRF - just reading the shortcut - Django REST Framework will intuitively give us a clue. It’s a REST Framework built on top of Django. Everybody knows that Django is one of the most popular web frameworks. Django REST Framework is a REST (REpresentational State Transfer) framework for Django. It was created to make API creation for Django-based applications extremely easy.
What is FastAPI
FastAPI is a micro-web-framework - so it’s much more lightweight than DRF. As a result, it’s great for creating fast APIs (especially for serverless applications). FastAPI is based on Pydantic and types hints to validate, serialize, and deserialize data. It supports asynchronous programming and can run with WSGI and ASGI servers for production.
Again, if you want to know more about FastAPI read our comparison between Flask and FastAPI.
Main benefits of DRF
Let’s examine the main benefits of using DRF for Python API projects.
It gives you a lot of elasticity and freedom when creating architecture. For example, you can quickly build simple APIs solely based on Django models (CRUD) and complicated REST systems as well.
- Easy to use
The main idea is to distinguish data representation transfer models (JSON or XML, for example) from views built on classes. As a result, those views are fast and can be easily modified and tailored to business needs.
It’s very efficient in comparison to other frameworks.
It’s very easy to set up and deploy.
It allows publishing metadata together with query sets.
It comes with a pretty advanced authentication module, allowing the engineer to easily distinguish authorized queries from unauthorized ones (REST vs. RESTrictive).
There is a pretty advanced engine that helps you with serializing data sources.
It’s been on the market for some time now, so there is a vast community and extensive documentation - it’s easy to find proven solutions to any problems that may arise.
Main benefits of FastAPI
Now let’s take a look at what FastAPI has to offer.
FastAPI is way faster than Django Rest Framework and is one of the fastest web frameworks for Python. The only more rapid frameworks are Starlette and Uvicorn (fun fact - FastAPI is built on top of Starlette).
It used to be challenging, but since Python 3.4 Async I/O was added. FastAPI allows out-of-the-box concurrency. You will also avoid creating event loops and async/await management. This will eventually impact your application.
- Documentation support
FastAPI generates fantastic documentation that is easy to read and use by frontend engineers and simplifies API endpoints’ testing. The documentation is browser-based, and it’s based on Swagger GUI.
- Dependency injection
FastAPI comes with a built-in dependency injection solution. It ensures that classes are not directly dependent on each other. Thanks to that, the changes in the code are easier to make. The modularity of the code is also increased, making it easy to scale. The path operation function allows engineers to declare relevant dependencies.
- Seamless central exception handling
To do exception handling, you just need to use the @app.exception_handler annotation or app.add_exception_handler function to register the response for an Exception, which will be managed automatically by FastAPI.
- Validation of data
Last but not least is the validation of data. It detects wrong data types and returns proper exceptions in JSON. Again, it uses the Pydantic module to simplify the process, making the code less prone to errors.
Drawbacks of DRF
Let’s now see both frameworks’ drawbacks - we will start with DRF.
- It's pretty big, so there is a bigger learning curve.
- There are some exceptions to following Django conventions you have to be careful about. For example, using validate_
instead of clean_ or handling validation in a single place instead of multiple ones.
- Websockets support could be better than other tools.
- Authentication methods could've been better maintained.
- No async support - hence a bit lower performance - and, in author’s opinion, DRF’s most significant disadvantage. You can naturally do some workarounds to make it happen, but that’s not the point.
Drawbacks of FastAPI
The last thing we need to look at is the drawbacks of FastAPI. There are not a lot of those, but it’s still important to take a look at them.
There is no built-in security system. Instead, you have to use fastapi.security module to manage it.
- Small community
FastAPI is relatively new (in comparison to Flask), so the community and amount of materials are a bit smaller. But this will undoubtedly change over time, so it’s not a huge issue.
So which should you choose?
In this comparison, both frameworks make pretty solid cases for each other. Hence the final decision may be harder to make. So let’s take a look at the best use cases for both frameworks (and please note, that’s just the author’s opinion):
Use DRF for reservation engines with a high load of data, e-commerce, or mobile apps that support web-based applications at the same time. Simply put - pretty much any full-stack, full-blown application.
As for FastAPI - since its main advantage is speed - it’s good to build API-oriented applications that take care of massive amounts of data. It’s faster than any other framework (probably that’s why it’s called FAST API).
The main difference between those two frameworks that may impact your decision-making process is that FastAPI is a MICRO framework, whereas DRF is a full-blown framework.