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:
django-jsonview
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.
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.
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.
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.
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 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:
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:
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.
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:
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.
Install LogRocket via npm or script tag. LogRocket.init()
must be called client-side, not
server-side
$ 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>
ElectricSQL is a cool piece of software with immense potential. It gives developers the ability to build a true local-first application.
Leptos is an amazing Rust web frontend framework that makes it easier to build scalable, performant apps with beautiful, declarative UIs.
Learn more about the 5 best JavaScript libraries for dealing with multidimensional arrays, such as ndarray, math.js, and NumJs.
We spoke with Dom about his approach to balancing innovation with handling tech debt and to learn how he stays current with technology.
One Reply to "Django REST framework alternatives"
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.