Why choose Django Rest Framework for API development

Outcomes matter so why choose Django REST Framework to develop and host APIs for your organisation? This article offers a few insights into the advantages we benefit from using django and the d-r-f framework for rapidly and reliably delivering core services for mobile, embedded, web and desktop client apps.

Infographic showing Django REST Framework with the Django automatic admin interface, coded in Python, storage in PostgreSQL, served by Apache or NGINX running on BSD or Linux for mobile, desktop and embedded clients including Angular, React, wxWidgets and Qt

Apps communicate many ways including APIs. Although there is a long history dating back to the 1940s, it wasn't until the year 2000 when Roy Fielding defined REST, and the APIs we know today came from his PhD dissertation "Architectural Styles and the Design of Network-based Software Architectures".

Today's Web APIs and in particular the "Representational state transfer" or REST is the glue that connects many disparate technologies together enabling rich, secure and sophisticated apps and services across the modern world. From any of Node, .NET, Java and Python on the server to iOS, Android, Windows, Linux, BSD, macOS on desktop and embedded clients, they can all speak fluently to each other thanks to Web APIs.

Of the many REST goals, the two this article will focus on are productivity and flexibility.

Productivity

Django-REST-Framework is both well integrated into Django and follows the same design principles by encouraging rapid development and clean, pragmatic design. 

Prototyping

There are many drivers for prototyping/rapid development including:

  • time to market;
  • faster to evaluate product market fit;
  • fluid user/client conversations;
  • cost.

Although Class-responsibility-collaboration cards are a useful brainstorming tool, it's the ability to rapidly generate a working prototype that brings significant advantages in demonstrating how something could work, confirming product market fit, ironing out workflow and usability issues before investing time and money in developing a fully fledged product.

Developer productivity

Django and the d-r-f delivers on the promise of helping developers take applications from concept to completion as quickly as possible.

Follow good practice and it's both secure and helps developers avoid many common security mistakes.

Django is one of the favourite frameworks according to the 2022 Stack Overflow developer survey with over 50% loving it. Python has a large user base and is affordably mid salary range at $71,105, though API developers command more.

Common tasks

Serializing data in an out, validating data, authentication and permissions are all handled in familiar ways between d-r-f and django so the learning curve is minimal. Documentation is excellent with many examples.

Flexibility

By its very nature portability is built into APIs enabling Android/iOS apps, Embedded systems, Traditional desktop apps and Software services to speak to Python / Java / C# backend servers. Here at Persistent Objects we use python's d-r-f for APIs and any number of clients including Angular for single page apps and wxWidgets for cross platform desktop and embedded apps.

Whether your style is Class based or Function based d-r-f offers both type of views.

Requirements change and apps need to follow. Django makes it easy to change a model, its view and its API. Robust model and change management infrastructure is provided by Migrations. Flexibility is built in so bespoke code and pre/post change processing can be easily added as required. This makes it easy to change, test, then deploy, (or reverse) as necessary.

Managing change

It's useful to identify api versions when coordinating documentation, integration, upgrading, testing and deprecation policies.

Taking a simple example of getting version information, then extending it to return more infomation:

Before After
GET https://api.example.com/api/v1/version
GET https://api.example.com/api/v2/version
{
    "version_major": 1,
    "version_minor": 0
}
{
    "version_name": "MyApp V1 beta",
    "version_major": 1,
    "version_minor": 0
    "version_revision": 0
}

Versioning an api makes identifying and managing change easier. The d-r-f makes it easy to deliver these changes.

Why the v prefix?

As documentation can get out of date, it's always helpful to be able to search original code, and why code is often preferable over web gui services that abstract away direct access to it. Anyone using grep will confirm it's easier and with far fewer false positives to search for '/v1/' instead of '/1/'.

Transformation from automatic admin interface to api

There are material benefits in blending the use of traditional django automatic admin interface pages, REST APIs and an outline Angular/React app. Django admin pages can be built in minutes and by sharing models and views, transition to an api via the d-r-f, also d-r-f serializers working in a similar way to django model forms makes it easier to develop, share code and transition from one to the other.

By leveraging django's automatic admin interface to manage content, you can focus on delivering key aspects of your app to materially reducing proof of concept or product launch time.

Automated testing

Ensuring stability as an app is developed is essential, as is test driven development transforming use cases into code. Django's test automation helps to solve, or avoid, a number of problems.

Scaleability

It's easy to add scalability with a cloud architecture, just add more cloud! Okay it can be a lot more complex and app design plays a significant part, but just adding a few more instances is an easy win.

Some of the busiest sites on the web leverage Django’s ability to quickly and flexibly scale.