Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django or Django Rest Framework

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?

like image 630
Rookie_123 Avatar asked Mar 05 '18 11:03

Rookie_123


People also ask

Is Django REST Framework same as Django?

Django is the web development framework in python whereas the Django Rest Framework is the library used in Django to build Rest APIs. Django Rest Framework is especially designed to make the CRUD operations easier to design in Django. Django Rest Framework makes it easy to use your Django Server as an REST API.

Should I learn Django or Django REST Framework?

It depends on your requirement and resources. If you only need a web application then django provides everything you require to develop a web application. If you need to develop a mobile application that will communicate with a backend server then you will have to implement Api.

Is Django REST Framework part of Django?

The Django REST Framework (DRF) is a package built on top of Django to create web APIs.

Is Django GOOD FOR REST API?

Django REST framework (DRF) 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.


1 Answers

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 tedious. 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.

like image 149
J. Hesters Avatar answered Sep 22 '22 13:09

J. Hesters