Programming

Difference between Django and Django REST Framework

Django vs. Django REST Framework (DRF)

Django and Django REST are used interchangeably in the development world. Even though these have very subtle differences, many people don’t know what exactly sets them apart and what are the main differences. In this article, we will be defining some concepts first and then diving into the differences and the similarities.

What is an API?

API, also known as Application Programming Interface, allows two computers to talk to each other. In simple terms, you hit an API endpoint with some parameters, and you are returned a response based on that.

Generic APIs used for testing don’t need any parameters and they return a response usually in a JSON format.

Let’s take a look at the following API:

https://catfact.ninja/fact

When you copy-paste the URL, you are hitting a URL and requesting a random cat fact as shown below.

Cat fact API
Cat fact API

There are other parameters that you can pass as well which are listed in the following home URL of the API

https://catfact.ninja/
Cat fact API home
Cat fact API home

APIs are very useful when you want to have a decoupled structure for your web application. You can host your API on a separate web server and have your front-end hit these endpoints and query the data that it requires.

It makes maintenance very easy and gives you the flexibility to choose whichever front-end framework you want.

What is a REST API?

A REST (Representational State Transfer) API is an API that strictly conforms to the REST constraints. It should be noted that REST is not a protocol or a standard.

In a REST API, a representation of the state of the resources stored is returned to the requester through an end-point. The information is usually transferred over these formats via HTTP:

  • JSON
  • HTML
  • Python
  • PHP
  • Plain text

Another caveat in REST API is that the headers and parameters are also important. They may contain identifier information including identity, authorization, etc. The headers may also use different methods like POST, PUT, and GET.

In order for an API to be considered a RESTFUL API, the following constraints must be met:

  • There should be client-server architecture where the resources are managed through HTTP.
  • Data should be cacheable which improves the client-server interactions.
  • The client-server communication should be stateless. Stateless means that there should be no information stored between requests. Each request is standalone and separate from other requests.
  • The interface should be uniform and information must be transferred over a standard form.

You get more information about REST APIs from Red Hat.

Difference between an API and a REST API

Every REST API is an API but not every API is a REST API. Think of the REST API as a subset of the API class.

API and REST API
API and REST API

Here are some of the major differences:

  • A REST API must strictly conform to the client-server architecture. On the other hand, APIs can follow an application to application format as well.
  • A REST API must be stateless. Normal APIs might not be completely stateless.
  • REST APIs are more scalable. Their modularity boosts their ability to scale more quickly without making many changes.
  • The Interface in normal APIs might not be uniform. This is because they don’t always follow the client-server setup.

What is Django

Django is a Python web framework that allows users to rapidly develop web applications by providing out-of-the-box functionalities. It removes the mundane tasks that are required for web development so you can focus on writing code that matters.

It is a full-stack solution. It has a web server while also a front-end solution that uses Django Templates. You can simply use Django to write a complete web application without using any other solution or framework. You don’t have to worry about the backend and frontend. You have a complete web application. There are no limitations here that you might find ONLY in Django REST.

What is Django REST Framework

Django REST framework is built on top of regular Django. It is a library that you have to import and use on top of your regular Django application and helps in making quick REST APIs.

The Serialization of DRF transforms your models into a response for a client very easily. It is so smooth it looks like magic.

When it comes to building REST APIs, it is far superior to regular Django.

Difference between Django & Django REST Framework

It should be noted that Django REST has a dependency on Django itself. In a nutshell, Django REST is a subset of core Django

Django & Django REST Framework
Django & Django REST Framework

As of April 2022, Django REST has a requirement of Django (2.2, 3.0, 3.1, 3.2, 4.0). It has to use the main Django application and is just a library that you import.

Django is a web development framework and Django REST is a library that facilitates in creating web APIs quicker and more easily. It helps make CRUD operations easier and makes it easy to use your Django server to serve APIs.

You can create REST APIs in Django as wellDjango Rest Framework just makes the whole process quicker, easier, and more scalable. 

Think of it this way; this library was created keeping in mind the core principle of developing Django itself i.e. to help developers avoid mundane tasks in their development. In this case, the development is focused on creating Web APIs.

Here is an example of creating a normal REST API in Django:

from django.core.serializers import serialize
from django.http import HttpResponse

class SerializedListView(View):
    def get(self, request, *args, **kwargs):
        qs = MyObj.objects.all()
        json_data = serialize("json", qs, fields=('name', 'address'))
        return HttpResponse(json_data, content_type='application/json')

Here is an example of using the Django REST framework:

from rest_framework import generics

class MyObjListCreateAPIView(generics.ListCreateAPIView):
        permission_classes = [permissions.IsAuthenticatedOrReadOnly]
        serializer_class = MyObjSerializer

As you can see, the work involved is drastically low. Even though we haven’t defined the serializer, the complexity is reduced and you need to do less in order to create a restful API. You can quickly incorporate views, authentication, lists, and much more without putting in more work.

Which one should you choose?

This depends on the requirements of the project. If you require a simple Django application and are going to use the in-built front-end solution in Django, then you don’t need to use DRF.

However, if you want to use other front-end technologies like React, going with DRF is a far better option and will make sure you take full advantage of the powerful REST architecture.

Shaheer Asif

Shaheer is leading the technical team at Computer Verge. He is very passionate about web applications and native software. He is a Computer Scientist but that doesn't stop him from following his passion: Art.
Back to top button