Toggle Menu

Insights > Tech Tips > Creating a RESTful API: Django REST Framework vs. Flask

Creating a RESTful API: Django REST Framework vs. Flask

There are a multitude of frameworks for creating RESTful APIs in just about every popular programming language. While it may be tempting to write a new API in an up-and-coming language such as Node or Go, it often makes sense to stick with a platform that your team is already familiar with. As a Python […]

By

April 15, 2016

There are a multitude of frameworks for creating RESTful APIs in just about every popular programming language. While it may be tempting to write a new API in an up-and-coming language such as Node or Go, it often makes sense to stick with a platform that your team is already familiar with.

As a Python developer, I prefer to stick with my favorite language when creating an API. There are more than 10 toolkits for creating RESTful applications in Python; I’m going to compare the two most popular options: Django with the Django REST Framework toolkit and Flask.

Django vs. Flask in a Nutshell

Django and Flask are both frameworks for creating web applications in Python. Django provides a full-featured MVC Framework that includes the whole kitchen sink. While Django alone could be used to make a RESTful API, Django REST Framework is a fantastic, feature-filled extension to the Django framework.

Flask is a micro-framework that follows the Unix philosophy of “do one thing and do it well”. Flask provides very little upfront, not even an ORM, but the community provides a large set of extensions that match a lot of Django’s feature set.

A popular idiom to compare the two frameworks is that ‘Pirates use Flask, The Navy uses Django.

What They Have In Common

While Django and Flask take different approaches to designing a web platform, they support a lot of the same capabilities when it comes to REST API design. This analysis assumes Django is using the Django REST Framework toolkit, and Flask is using various popular plugins necessary to achieve comparable results.

User Auth
Django REST Framework supports using Django’s built-in user model for API authentication and authorization (docs). With Flask, you can use built-in tools for basic auth, or third party plugin such as Flask-HTTPAuth for more complex scenarios. Miquel Grinberg provides a thorough tutorial for creating API endpoints using Flask-HTTPAuth.

Rate Limiting
Django REST framework supports throttling of both anonymous and registered users (docs). Flask, along with the Flask-Limiter plugin, support a similar feature set. Both platforms support in-memory, Memcached, or Redis backends for store rate limitation data.

Relational Database Mapping
Django REST Framework provides a straightforward method of mapping Django ORM models to API endpoints (docs). Flask, along with Flask-Restless, provides similar capabilities for an SQLAlchemy database model (docs). Both frameworks provide a simple path to creating a CRUD interface for an existing data store.

Advantages of Django with Django REST Framework

Versioning
Handling multiple versions of an API is never a simple endeavor. Django REST Framework includes support for versioning is fairly flexible, making the task a little less onerous. The toolkit supports multiple URL formats (example.com/v1/sample/, v1.example.com/sample/, or example.com/sample/?version=v1), and the version is passed in as a request parameter (request.version) to be used in a view.

Versioning can be handled in Flask, but it has to be managed by the developers. In fact, many solutions involve structuring the entire hierarchy of the applicable to support the concept of versioning.

Browsable API
Django REST Framework generates HTML pages to browse and execute all API endpoints. With this feature, users and/or developers can execute GETs and POSTs quickly and easily in their browser. Even if disabled for production use, this is a fantastic feature for testing and demos.

Flask does not have a good Browseable API option. The creator of Django REST Framework created a similar library for Flask called Flask-API, but it is not under active development nor is it ready for production use. Another option is to use the swagger API wrapper in front of the API, but maintaining the swagger schema would take time.

Regular Releases
Django and Django REST Framework are both under heavy active development. Django has a major release twice per year with an LTS release every two years. Django REST Framework has had three major releases in the last year. Flask, on the other hand, last had a tagged release in 2013. This does not mean the project is dead, as the multitude of plugin libraries receive regular updates, but the contrast with Django is noteworthy.

Advantages of Flask

Speed
Flask is able to achieve generally faster performance than Django, generally due to the fact that Flask is much more minimal in design. While both of these frameworks can support several hundred queries per second without breaking a sweat, neither were designed with a priority on speed. Falcon and Bottle are Python web frameworks that emphasize speed, albeit with much smaller developer communities, and are worth a look if speed is more important than support and features.

NoSQL Support
The Django platform heavily ties itself to relational database integration. An extension called django-rest-framework-mongoengine wires Mongo into Django REST Framework, but I can’t speak for its ease of use. Because Flask has no built-in ORM, it is more freely able to integrate with NoSQL databases like MongoDB (via mongokit or Flask-Pymongo) and DynamoDB (via Flask-Dynamo). In addition, the Eve toolkit, a wrapper on top of Flask, includes built-in support for MongoDB.

Summary

While it may be more adventurous to be a Pirate, it’s tough to beat the Navy’s arsenal. However, pay attention to the specific needs of your API when choosing a framework. Django REST Framework’s browseable API, versioning support, and well thought out design is fantastic if you want it, but Flask’s minimalism might make a lot more sense if you’re planning several microservices or require NoSQL integration.

You Might Also Like