Django or Django Rest Framework Django or Django Rest Framework django django

Django or Django Rest Framework


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 serializefrom django.http import HttpResponseclass 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 genericsclass 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.


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.


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.