August 27th, 2017
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?
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?
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:
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
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:
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?