본문 바로가기
OLD_달려라/호기심천국

Django REST framework VS Django

by 달승 2020. 11. 27.

 

Django REST framework is a powerful and flexible toolkit for building Web APIs.

Some reasons you might want to use REST framework:

 

Home - Django REST framework

 

www.django-rest-framework.org

 

 


 

 

 

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

 

 

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

stackoverflow.com

 

댓글