Why I love Django & Django REST Framework

If you’re reading this article, it is very likely you are a serious Python developer or at least dabble with the language. If you happen to be a web developer, you probably have already used or at least heard about Django. Django is a pretty awesome web framework. And in this post, I am going to highlight a few key features that I absolutely love about Django. Along with Django, I will also discuss why I love DRF, the very popular REST API development framework for Django. Lately, these two have become indispensable parts of my life as a web developer and I feel it’s very important that I credit them for their awesomeness and spread the words!

Why I love Django?

  • I can quickly generate a project skeleton using the django-admin command. There is management commands to generate apps too.
  • I absolutely love Django’s project structure. It is very well organized and meaningful. A project is composed of reusable apps and the apps can self contain the codes and resources they need/use. The idea of reusable apps is very handy. There are plenty of open source apps that you can plug into your project and extend them as necessary. This allows us to reuse codes and get things done faster.
  • Django’s way of handling database access makes perfect sense to me. I define models, then automatically generate migrations from those. The migrations are written in Python, so I can adjust or tweak if necessary, programmatically. The Model definitions can be introspected by other parts of the framework to generate Admin UIs or API end points automagically – this is a huge win as we will see.
  • Django has a very powerful DB routing mechanism that allows us to use multiple databases and programmatically control the access to them. We can totally control which database is read from, which database is written to, for each app and each model.
  • The URL routing portion also makes great sense. There’s a main route configuration per project. We can add app specific routes to this configuration by include-ing them. You can add namespaces to these included ones. The namespacing gets rids of naming conflict when importing routes from 3rd party apps.
  • For simple views, the function based views are pretty efficient. But there’s also class based views for more control and dealing with complex needs.
  • The Generic Class Views are awesome! You can define ListView, CreateView, UpdateView, DetailView or a DeleteView. You pass them the Model to use and some configuration options, they will handle your CRUD operations for you while you can focus your time and concentration on preparing the templates. These have allowed me to build custom CRUD views very very fast. The generic views are very customizable, so you can alter the database queries, pagination, form fields, values passed to the template. If you’re still writing your own business logic for CRUD views, it’s time you took generic class views for a ride. What I like most is that generic views are generated dynamically. It’s not like scaffolding or code generation. In the case of scaffolding, when you need to add an extra field, you need to modify your model, controller, template, form etc. In class based generic views, you just update your model and everything is updated dynamically.
  • Talking about CRUD views, often you won’t even need to build them. The Django Admin interface takes in your models and generates a beautiful (and powerful) admin interface from them. The admin interface is very customizable. You can add custom actions, filtering, templates and whats not! Once you master Django’s admin interface, you will probably never need to build CRUD views from scratch. Many of my projects use Django Admin with high client satisfaction. And remember what we said about reusable apps and open source packages? There are several packages which provide alterations or replacement for the built in admin interface. You can plug in one of those for a different taste!
  • Management commands are easy to write and comes in very handy for simple tasks. When you install apps, they can provide useful management commands too. One of my favourite management command is “dbshell” which allows me to drop into the database’s command line without remembering the login details.
  • Django Templates are pretty awesome too. The syntax is quite similar to Jinja2 (which inspired many templating languages across different platforms). You can extend the templates by writing your own tags and filters. And there’s of course a backend for direct Jinja2 support.
  • Django is quite mature and the eco system is thriving. There’s a django package for almost everything you would need. There are many 3rd party packages for extending the different parts of Django. There are blogs, forums, cms etc apps which you can plug into your current project to add those functionality. Python also has a vast libraries for numerous purposes. It’s easy to plug them in when we need.
  • Oh, and did I forget to mention the documentation? Django documentation is superb. Very well written and covers almost everything in the framework. I would say Django Docs is one of the best out there.

The above mentioned features are just a few that I can remember right now. Django has been very pleasant to develop in. I have always felt a productivity boost as well as mental satisfaction when working with Django. Every time I faced a challenge, I have found a very simple solutions to it.

Why I love Django REST Framework?

  • Browsable APIs on the web – this is one of the best things about DRF. DRF provides very nice web browsable views for your API end points. These views allow HTML forms to submit data to the APIs. At the same time, they also show sample JSON payload for those requests. You can submit a request through the forms/json input and get back a response. So this is pretty much self documentation of the APIs. I have worked with front end and mobile developers (who consumed my APIs) and they absolutely loved the web browsable interface. In most cases, I didn’t even need to provide any API docs to them, they managed to figure things out for themselves. And if you would still want Swagger, there are 3rd party extensions for that too.
  • The ModelViewSet is like those admin views for Django. You can quickly generate CRUD based REST APIs out of your models using this class.
  • There is APIView which provides get, post, put, delete etc methods representing the HTTP verbs. You can extend APIView and provide business logic for these methods to quickly craft your APIs. There are mixins and some preset class based views to generate these methods based on querysets.
  • But don’t be satisfied just yet, ViewSets take things one step further. When you use APIView, you need to create two routes (for example one for /users and one for /users/1) but with ViewSets allow us to focus more on the business logic while forgetting about the route management. ViewSet is a class based view that has methods like list, retrieve, create, update, destroy – instead of the http verbs. The ViewSet methods allows us to cover all the REST operations in just one class. We add the view set to a router and the router generates all the necessary underlying django routes, ready to be included in a urlconf. And as mentioned earlier, the ModelViewSet is a ViewSet that generates these methods from a Model (queryset)
  • Authentication is baked in the framework. It’s very easy to setup different authentications for your APIs. The permission management is also very simple. The framework integrates nicely with some third party packages to support authentication methods which are not supported by the framework by default.
  • The framework is very customizable. We can easily extend the functionality to suit our needs. It’s all very simple, quite like the simplicity of Django!
  • The documentation is excellent. Adequate examples and very clearly described.

The only thing I am not completely happy with DRF is creating nested routes (/user/1/comment/2) is still pretty difficult/cumbersome. But the way DRF adds features quickly, I hope this will be resolved soon, in some upcoming releases!

Helping These Projects Grow

Django and DRF both are open source projects and are the results of many hours of dedication from kind hearted OSS enthusiasts. Today, we can build and maintain awesome projects because Django and DRF exists. We should all consider giving something back to these projects, in return to the benefits they have brought us!

We can help these projects by contributing codes, helping with translations, answering questions, promoting the projects or making donations.

Here you can learn how to contribute to Django — https://docs.djangoproject.com/en/dev/internals/contributing/

Django Software Foundation accepts donations here — https://www.djangoproject.com/fundraising/

DRF Contribution guide here — http://www.django-rest-framework.org/topics/contributing/


Leave a Reply

Your email address will not be published. Required fields are marked *