Timothy Olaleke Tim is a software developer, DevOps enthusiast with a passion for automation, and open source hobbyist. In his free time, he writes Google Cloud-related tutorials, makes open contributions on GitHub, and participates in developer communities.

Django REST framework alternatives

3 min read 899

Django REST Logo

In this tutorial, we’ll introduce you to some alternatives to the Django REST framework for building web APIs. We’ll focus three libraries that support extending applications with a RESTful API that uses HTTP requests to access and use data: Django Tastypie, Restless, and Django JSON View.

We’ll cover the following in detail:

What is Django?

Django is a Python-based, free, open-source web framework that follows the model-template-views architectural pattern. It reduces the hassle associated with web development so you can focus on writing your app instead of reinventing the wheel.

What is a REST API?

A REST API is a popular way for systems to expose useful functions and data. REST, which stands for representational state transfer, can be made up of one or more resources that can be accessed at a given URL and returned in various formats, such as JSON, images, HTML etc.

RESTful APIs use HTTP requests to access data. That data can be used to GET, PUT, POST, and DELETE data types, which refers to the reading, updating, creating and deleting of operations concerning resources. These are known as CRUD operations. Data formats of the REST API can also include application, JSON application, XML, etc.

What is Django REST framework?

Django REST framework (DFR) is a powerful and flexible toolkit for building Web APIs. Its main benefit is that it makes serialization much easier.

Django REST framework is based on Django’s class-based views, so it’s an excellent option if you’re familiar with Django. It adopts implementations such as class-based views, forms, model validator, QuerySet, etc.

Should you use Django REST framework?

If you’re not so familiar with Django principles, you might want to explore other options before you spend the time learning a new web framework. In this guide, we’ll review the best alternative tookits to the Django REST framework.

To demonstrate, I built a small, basic application to store names and ages of my friends. We’ll integrate the RESTful API frameworks with this application.

You can clone the code for our demo Django application from GitHub. The code snippets would also work seamlessly in existing projects.

Django REST framework alternatives

Three of the most popular Django REST framework alternatives are Django Tastypie, Django Restless, and django-jsonview. We’ll examine each in detail.

Django Tastypie

Django Tastypie is a webservice API framework for Django that provides a convenient, yet powerful and highly customizable abstraction for creating REST-style interfaces. It’s a perfect solution if you:

  • Need an API that is RESTful and uses HTTP well
  • Want to support deep relations
  • Don’t want to write your own serializer to make the output right
  • Want an API framework that has little magic, is very flexible and maps well to the problem domain
  • Require XML serialization that is treated equally to JSON (and YAML is there too)

To install Django Tastypie:

$ pip install django-tastypie

Here’s a Django Tastypie code sample:

# api/tastypie_resources.py
from tastypie.resources import ModelResource
from .models import Friend

class FriendResource(ModelResource):
    class Meta:
        queryset = Friend.objects.all()


# urls.py
from django.urls import path, include

# Tastypie
from tastypie.api import Api
from api.tastypie_resources import FriendResource
v1_api = Api(api_name='friends')
v1_api.register(FriendResource())

urlpatterns = [
    path(r'api/v1/', include(v1_api.urls)),
]

Visit your localhost to see the JSON response from the API:

JSON Response in the API

Django Restless

Django Restless is a lightweight REST mini-framework for Python. It works great with Django, Flask, Pyramid and Tornado and is useful for many other Python web frameworks. Restless has a small, flexible and fast codebase with default output as JSON.

Django Restless offers a fresh take on REST frameworks. While other frameworks attempt to be very complete, include special features or tie deeply to ORMs, Restless focuses on the basics.

To install Django Restless:

$ pip install restless

Below is a Django Restless code sample:

# api/restless_resources.py
from restless.dj import DjangoResource
from restless.preparers import FieldsPreparer
from .models import Friend

class FriendResource(DjangoResource):
    preparer = FieldsPreparer(fields={
        'id': 'id',
        'age': 'age',
        'name': 'name',
    })
    # GET /api/v2/friends/
    def list(self):
        return Friend.objects.all()
    # GET /api/v2/friends/<pk>/ 
    def detail(self, pk):
        return Friend.objects.get(id=pk)

# urls.py
from django.urls import path, include

# Restless
from api.restless_resources import FriendResource

urlpatterns = [
    path(r'api/v2/friends/', include(FriendResource.urls())),
]

You can preview the JSON response on your localhost.
Preview of the JSON Response

django-jsonview

django-jsonview is a simple decorator that translates Python objects to JSON and ensures that your view always returns JSON.

django-jsonview only needs you to add a decorator to your view method and it returns a JSON.

To install django-jsonview:

$ pip install django-jsonview

Here’s an example of django-jsonview code:

# api/views.py

from jsonview.decorators import json_view

@json_view
def my_view(request):
    return {
        'foo': 'bar',
    }

# urls.py
from django.urls import path, include

# JSON View
from api.views import my_api_view
urlpatterns = [
    path(r'api/v3/friends/', my_api_view)
]

You can preview the API response on your localhost:

Preview of the API Response

Conclusion

In this article, we explored three solutions for building RESTful APIs using Django.

Check out the Django docs to learn more about Django packages for building APIs and REST APIs.

Get setup with LogRocket's modern error tracking in minutes:

  1. Visit https://logrocket.com/signup/ to get an app ID.
  2. Install LogRocket via NPM or script tag. LogRocket.init() must be called client-side, not server-side.
  3. $ npm i --save logrocket 

    // Code:

    import LogRocket from 'logrocket';
    LogRocket.init('app/id');
    Add to your HTML:

    <script src="https://cdn.lr-ingest.com/LogRocket.min.js"></script>
    <script>window.LogRocket && window.LogRocket.init('app/id');</script>
  4. (Optional) Install plugins for deeper integrations with your stack:
    • Redux middleware
    • ngrx middleware
    • Vuex plugin
Get started now
Timothy Olaleke Tim is a software developer, DevOps enthusiast with a passion for automation, and open source hobbyist. In his free time, he writes Google Cloud-related tutorials, makes open contributions on GitHub, and participates in developer communities.

One Reply to “Django REST framework alternatives”

  1. Why not something other than django entirely? Starlette and fastapi are doing pretty awesome things. Anything other than async is going to be way behind the times at this point.

Leave a Reply