80
votes

I have made a certain app in Django, and I know that Django Rest Framework is used for building APIs. However, when I started to read about Django Rest Framework on their website, I noticed that each and every thing in API Guide(like request, response, views etc.) claims it is superior to Django (request, response, views etc.).

The thing I am not understanding is whether these APIs will replace my existing Django models, views etc. or how can I use them differently in my existing Django code?

I am quite familiar with Django, but not able to understand exactly what Django Rest Framework is even after spending some time on it. (I know it is used for APIs.) Also, do I actually need an API? My app is able to send data to the server without an API, so in what case would I need an API?

8
They are designed to work together. DRF can build on top of your Django models (and Django authentication etc) - you use it instead of plain Django views in the places where you want API endpoints - Anentropic
@Anentropic So If I build my app entirely in DRF then both the things will be taken care of (The building of app itself along with API thing) - Rookie_123
try reading the DRF docs, eg the tutorial here django-rest-framework.org/tutorial/1-serialization/… ...it shows how you create a Django model and then DRF serializers based on the model, and then views and then an API based on the model and serializers - Anentropic
"also do I actually need an API" ...if you don't know whether you need an API you probably don't need an API - Anentropic
it shows you how the concepts fit together by giving a practical example... you can see the Django parts and the DRF parts and also goes from basic to a bit more advanced. I suggest to actually read it and try and understand what you are reading - Anentropic

8 Answers

42
votes

Django Rest Framework makes it easy to use your Django Server as an REST API.

REST stands for "representational state transfer" and API stands for application programming interface.

You can build a restful api using regular Django, but it will be very tidious. DRF makes everything easy. For comparison, here is simple GET-view using just regular Django, and one using Django Rest Framework:

Regular:

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=('my_field', 'my_other_field'))
        return HttpResponse(json_data, content_type='application/json')

And with DRF this becomes:

from rest_framework import generics


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

Note that with DRF you easily have list and create views as well as authentication.

30
votes

Whether you need an api depends on what you want to do. For example, if you want to access everything in your Django models from a mobile device AND a web-app, you would want to use DRF.

Why? Consider the case that you are developing an iOS app that users can sign into and you want to use Django as your backend. If you also want to have a website that they can use to change around their app's profile information (say, update their email address or upload a picture), you would need a way to share the information in your Django models with both a website and the iOS device. How can this be done? By giving the user the ability to create/read/update/delete data by simply telling it a url to go to. Now, if you want to access information from the model you can do it from multiple devices, since any device can visit a url.

However, if you're just building a simple webapp/webpage and keep it all in one place, you can just use straight Django.

Side note: it's a pretty popular opinion that you should try to separate your frontend from your backend as much as possible. In this case, if you want to use some frontend development framework like React, Angular, or Vue, it's gonna get messy trying to include all those resources in the Django template pages even if you only want a simple web app/web page. In this case, you would use DRF to set up your backend, and just hit the urls from the frontend using a tool like axios. In this scenario, your frontend would likely be hosted on something like Node.

Again, what you decide to use really just depends on what you want out of your app/site and how comfortable you are with each tool.

13
votes

The basic purpose of DRF is to provide APIs. APIs are the touch point in an app. Imagine a project where you have a Django wizard and someone on your team is a JavaScript expert and you want to develop mobile app. The JavaScript expert talks about web elements and threading and the Django expert talks templates and ORM, now what? API is your answer. Let Django wiz give APIs and JSON and let JavaScript wiz do his front end magic. Simplest answer I can think of.

4
votes

In simple words,

Django : It provide features for standard web app

Django Rest Framework: It provide features to build standard REST API.

1
votes

Here is a link which might give you some insights on what you are looking for. This explains how a django app can be converted into Django Rest Service, and it also explains what a REST Service actually is

https://opensourceforu.com/2017/11/converting-django-app-rest-api/

1
votes

Simply saying: with just Django you can create server side rendered websites; and with DRF you can create SPAs with a vuejs or react frontend part.

1
votes

the old school used Jinja2 with html5, css3, but this became inconvenient when Single Page Applications became popular.API helps to provide information more convenient for the frontend programmer. You no longer work with the frontend. The API is convenient when developing as Web applications and when developing applications for the phone.

1
votes

Let's sum up,

Django :

Tightly coupled with Django Templates.

Django Rest Framework :

Open for building views of my own choices with REST APIs.