/ programming

Do you want to use Django for REST API? Consider it.

Every Python developer knows Django. It's a great Python framework which includes everything that's needed to start a project. It was the first framework I got to know when I started learning Python. For a long time, I was convinced it's the best framework and I should use it for everything.

I was working on projects where Django was used as the main framework. At some point, I decided to try other Python frameworks. I tried Pyramid, Flask, Bootle, CherryPy, Tornado, webapp2. I was using Pyramid, Flask, and webapp2 in real, commercial projects, the other frameworks were only used for my small private projects, just to try what they offer.

I was undecided what I should use. Every Python framework has advantages and disadvantages but I don't want to write about that. About 1.5 year ago, I've found another Python framework, Falcon. It's simple, small and flexible. I've started using it in my projects and after some time, I asked questions myself: Why should I use Django (or any other Python framework) for REST API? What Django give me?

The answer was not simple. I know that Django has batteries included and after installing it you have a ready project and you can start coding. Let's think about it. For the last 2-3 years, I was working on projects where we were using REST API written in Python on the backend and Javascript on the frontend side.

Using Django you get a lot of built-in modules like ORM, serialization, templates, forms, sitemaps, sessions, etc. Do you need everything? I don't. I really like to have only the things which I need and add new ones if I need them.

If you use Django and need to add some new feature you have two choices. You can implement it yourself or use Django application maintained by the community. The question is, is it a good idea to use code which is created to work for everyone? Do you really need everything from this application? Does everything work for you or maybe you have chosen this application because it will be faster to implement a new feature for your client? What if you need to change how this application works?

It never happened to me for example, that default User from Django was sufficient. I always needed to override some things and add a lot of other stuff.

It never happened to me that third party Django application met my expectations and sometimes it was not easy to change particular things.

There is one important thing. We want to have REST API. Django doesn't support it. We have to install an additional application like Django REST framework.

Why I like Django?

You can install it, set up the project in 5 minutes and start working. The admin panel is also a nice feature (but I wouldn't use it as a panel for users).

Why I don't like Django?

  1. Everyone wants to use it for everything. When it comes to choosing a new project stack, most everyone says: let's use Django because it's easy, has a good documentation and we know how it works.
  2. When it comes to adding a new feature the first questions is: is there any application for Django which gives us this functionality?
  3. In the Django world, it looks like it's easier to get some a Django application and override some things instead of writing your own module. Sometimes I don't get it because 90% of the applications features might not be needed but still, it's a good option because it will be faster to do what we want.
  4. Some time ago I was laughing at the "Django developer" term. Now, it's not funny. When I was working on a Django project I heard a lot of questions like "How to do this in Django?" instead of "How to do this in Python?" Maybe I exaggerating but there is something to it.
  5. Django is not as flexible as I want. You have to build everything the Django way.

Why I like to use Falcon?

Falcon is a very small Python framework. If Flask is a microframework, Falcon is a nanoframework ;-) I think you can read the whole documentation in one hour. It provides the most important things.

In Falcon we don't have user management, we have to write it ourselves. Is it difficult to do this? I don't think so. It's basic stuff. We can use PyJWT for user authorization. Yes, we will need to add our own middleware and some views but the most important part is done by PyJWT. For email sending we can use Python SendGrid client, implementation is very easy. As a database ORM we can use SQLAlchemy together with Alembic for migrations. Elasticsearch - elasticsearch-py, elasticsearch-dsl-py. Celery - we can use just Celery from pypi. For data validation, we can use marshmallow.

The python packages listed above are just an example. We can use different Python applications, it's up to us. What I want to show here is flexibility. If you like SQLAlchemy, use it. Don't like SQLAlchemy? You can use peewee. Don't you like Marshmallow? You can use colander.

Of course, you say that using Falcon you have to write a lot of things from scratch and you need more time. You're right, but is it an issue? Do you want to write your project as fast as possible or write it properly? It's not entirely true that you have to write everything from scratch. Let's use a JWT authorization as an example. You can use PyJWT so you don't have to write whole JWT functionality. Create User database model to store username and password, create Falcon middleware to read Authorization header and use PyJWT here. Add some view to allow a user to generate a token and refresh it. That's all. You have the basic authorization. The challenge here is how to implement and use Python package, not how to write this from scratch.

Yes, it takes more time than installing Django application but I see several advantages here:

  • you have to think about your solution, instead of writing pip install django-foo, you have to plan what you need, how it should work, what problems you can encounter, how to write your solution to be able to extend or change it in the future without bigger problems
  • you know how your system works because you have written it and because of that it's easier to debug it
  • you only have code which is needed, nothing more

Several times I saw that someone had used Django application and after some time it turned out that this application is not maintained anymore. What now? We had cases where Django application was used only because it was faster to implement something using it. When it comes to removing it from a project it turned out that it was hell. More than once I saw that someone created a fork of Django application, modified it and used it in a project because the original project wasn't enough. Terrible idea.

Should I use another framework than Django?

It depends on you and your team. You should ask yourself some questions:

  1. Do you need Django?
  2. Can you use something other than Django?
  3. Why do you want to change Python framework?
  4. Implementing new features could take more time. Is it a problem for you?
  5. Do you have team members who can or want to use something different than Django? You don't want to hear "OK, I don't know how to do that, it was easier in Django." every day.
  6. Will it be a problem for you to spend more time on learning new things? Even if it will be hard to understand at the beginning?


I don't want to say that Django is a bad Python framework and you shouldn't use it. I want to say that you have a choice. For me, the Django framework was a good option when I was rendering everything on the backend side and I was using most of the functionalities which Django gives. Currently, I like flexibility, I only want to have what I need, I want to have total control over on how my project works. I want to choose what I want to use and how I want to implement it. It's easier for me to use python package and integrate it with Falcon instead of installing Django application and searching how to change parts which don't work the way I want.

What's your opinion?