Django DRF

Django Rest Framework, which is also referred to as DRF, is a very powerful application to build RESTful APIs in a Django application. DRF uses the Django framework. Similar to Django, DRF is also an open-source network.

We can create APIs with minimal effort and quick time with Django DRF. DRF’s documentation is considered by many to be better than Django. We can make highly customizable APIs with DRF, which supports authentication, authorization, pagination, sorting, etc.

Why DRF?

  • We can integrate authentication with technology like OAuth 1a and OAuth2.
  • Serialization is also included, which can support ORM.
  • Django DRM has great community support in case we are stuck with a problem.
  • Django Rest Framework is highly customizable.
  • Many giant tech companies are using it, like Mozilla, Heroku, and RedHat.

What is an API?

API stands for Application Programming Interface. API is used to communicate between two applications. It is software that is used by another software to provide some information.  In Django, API acts as a mediator to other applications like any data source, android, web browsers, etc.

The API is mainly used in a backend to fetch the data from other sources like a server or a database and provide it to the application. The data which API retrieves can be in many formats like XML, JSON, etc., but JSON is very common and easier to work with as a web developer. 

There are many APIs example, on the internet, there is an API for mostly all the tasks,  github has an API for the repositories, there is an API for random names, Google has an API for integrating Google Maps.

RESTful API

REST stands for Representational State Transfer. REST is an architectural way of designing network applications. Any data that the client is retrieving through REST can be fetched/edit/delete. REST defines every operation with a specific URL.

A RESTful API can be called a mixture of REST and API, which acts as a mediator between two computers over a web service. The difference between an API and RESTful API is that RESTful API communicates on a RESTful Web Service.

Requirements

We need to have certain programs and software to use the Django Rest Framework. The requirements are:

  • Python (3.5 or higher)
  • Django (2.2, 3.0, 3.1)

Installation

We can install the Django REST framework by using python package manager pip. To install the DRF, use the command below:

pip install djangorestframework

Now, we have to specify to Django that we are using the REST framework by adding it into the INSTALLED_APPS section of the settings.py file.

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'talk',
    'rest_framework'
)

We can also add the following code to the urls.py file:

urlpatterns = [
    ...
path('api-auth/', include('rest_framework.urls'))
]

We have successfully installed and integrated the Django REST framework into our project.

Usage

Now, we will see how to use the DRF in the application.

First of all, we have to configure the settings for REST_FRAMEWORK in our settings.py file.

REST_FRAMEWORK = {
    'DEFAULT_PERMISSION_CLASSES': [
        'rest_framework.permissions.DjangoModelPermissionsOrAnonReadOnly'
    ]
}

Add the following code in the urls.py module:

fromDjango.urls import path, include
fromDjango.contrib.auth.models import User
fromrest_framework import routers, serializers, viewsets




classRest_Serializer(serializers.HyperlinkedModelSerializer):
classModel:
model = User
fields = ['url', 'username', 'email', 'is_admin']


classREST_View(viewsets.ModelViewSet):
users = User.objects.all()
new_serializer = REST_Serializer


router = routers.DefaultRouter()
router.register(r'users', REST_View)




urlpatterns = [
path('', include(router.urls)),
path('api-auth/', include('rest_framework.urls', namespace='rest_framework'))