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'))

Related Topics

Django Admin

To manage the content of the website, e.g., adding and deleting posts, web developers need an admin portal on the website. The admin portal allows trusted users, also knows as...

3 minutes read.

Django Framework

Django is a popular high-level open-source web development Python framework based on follows Model View Template (MVT) architecture that allows rapid development. It. It provides common functionality like Admin Interface,...

3 minutes read.

How to Uninstall Django from cmd

Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a faster way....

3 minutes read.

Django Filters

Django is a high-level python based web framework. Django is mostly used as a backend engine for websites. Nowadays, every website communicates to a database to fetch all the information...

3 minutes read.

Django Model

Django Model A template is a class representing a table or collection in our DB and where each class attribute is a table or collection area. In the app / models.py, models are...

6 minutes read.

Django CMS vs WordPress

Both Django CMS and WordPress belong "Self-Hosted Blogging" or "Content Management System (CMS)" category, that is used to manage digital content. Bloggers usually use these platforms. Django CMS Django is one of...

4 minutes read.

Django Class Based Views

Class-based views were developed to solve the problem of customizing function-based generic views. We can arrange our view code Object-Oriented by using class views. Class-based views do more than function-based...

3 minutes read.

Django Create Superuser

For managing the website, we need an Admin while creating the Django application. In order to create the admin user, create superuser command is used. The superuser is called the...

3 minutes read.

Best Django courses

In this tutorial, we will see various courses in python Django available on the internet. Django has become popular these days, so courses on it are also increasing day by...

7 minutes read.

Django Messages

Django Framework Django is a framework that can make web applications more accessible and faster. Django is an open-source collection of python libraries, and Django can be used for both the...

6 minutes read.

Django Environment Setup

Django is a free and open-source web framework, which uses Python as its programming language. It is based on Model View Template (MVT) architecture. A framework is a collection of...

4 minutes read.

Uses of Django

Django is a free and open-source Python-based web framework, which offers great functionalities to its users. Django was developed in 2003, and the final version was released in 2008 by...

3 minutes read.

How to create an app in Django

Create an application in Django Django is famous for its unique and fully managed application structure. For each functionality, an application can be created as a completely independent module. This article will take...

2 minutes read.

Django Channels

Channels is a project that uses Django and adds some functionalities beyond the traditional HTTP by using WSGI and ASGI.  We can use both synchronous and asynchronous requests with ASGI...

5 minutes read.

Django Installation

Django Installation Django development environment consists of installing and setting up Python, Django, and a Database System since Django deals with a web application. Below the statement will guide you through installing Python 3.8.0 and...

2 minutes read.

Django Template System

Django Template System Django allows Python and HTML to be divided, the Python goes into views, and HTML goes into templates. Django relies on the render feature and the language of the Django Model...

5 minutes read.

Django Create Project

Django is an open-source free to use web framework which is based on Python. Django uses Model-View-Template as its architecture. The final version of Django was released in 2008. Django...

4 minutes read.

Django Authentication

Django is an open-source free to use web framework which is based on Python. Django uses Model-View-Template as its architecture. The final version of Django was released in 2008. Django...

4 minutes read.

Django Model Fields

In Django, if we want to store the information about anything, we use Models. It contains various fields about the data. Generally, each model represents a database where all the...

4 minutes read.

Django vs Laravel

Django Overview Django is a Python Programming Open Source framework for web development. It was released in 2008 and was developed by two web developers named Adrian Holovaty and Simon Willison....

4 minutes read.