Django Rest Framework

Manish Sangwan   30 January,2020  

Django REST Framework Tutorial – Feel Blessed!! ‘Coz Boss wants you to REST

 

We will learn to build a REST API in Django through a very simple method. Just follow the below steps and your first API will be ready to get going with minimum code.

Django REST Framework (DRF) allows developers to rapidly build RESTful APIs. We will see how DRF is actually used and also clear some very basics of the web. This article assumes though, that you have a basic understanding of the working of the web. We will learn topics like Django for APIs, RESTful web services and then build up to DRF.

Okay, so buckle up for this article.

What is an API?

Before understanding what DRF can do we first need to understand what is an API. API is an acronym for Application Programming Interface. Two machines use it to communicate with each other. We will be dealing with Web APIs and then the definition changes to:

An API is used by two applications trying to communicate with each other over a network or Internet.

The API acts as a mediator between Django and other applications other applications can be from Android, iOS, Web apps, browsers, etc. The API’s main task is to receive data from other applications and provide them to the backend. This data is usually in JSON format.

What are RESTful APIs?

Well, first we need to understand what is REST. Don’t worry, I will explain it in simple words.

REST stands for Representational State Transfer. REST is an architecture on which we develop web services. Web services can be understood as your device connects to the internet. When you search for anything on Google or watch something on YouTube. These are web services where your device is communicating to a server. When these web services use REST Architecture, they are called RESTful Web Services. These web services use HTTP to transmit data between machines. now, back to the question, what are RESTful APIs?

A RESTful API acts as a translator between two machines communicating over a Web service. This is just like an API but it’s working on a RESTful Web service. Web developers program REST API such that server can receive data from applications. These applications can be web-apps, Android/iOS apps, etc. RESTful APIs today return JSON files which can be interpreted by a variety of devices.

What is Django REST Framework?

So, as we learned in previous sections, DRF is an acronym for Django REST Framework. (stating the obvious) It’s used to develop REST APIs for Django. Yup, DRF is used to develop RESTful APIs which is both easy and a smart way.

DRF is a framework built upon the Django Framework. It is not a separate framework. You can say that it is a tool which alongside Django is used to develop RESTful APIs. It increases the development speed. It also addresses various security issues natively.

Now, we need to install Django REST Framework. To install it, execute this command.

pip install djangorestframework

Now, open settings.py file of your project. There install this app, ‘rest_framework’. Just include them in the INSTALLED_APPS list.

Now, update models.py:

Also, register this model with django-admin. Add this code to admin.py file.

Ok, now we need to migrate our changes in the database. Execute these two commands in the terminal.

python manage.py makemigrations

python manage.py migrate

After that, our first part of DRF comes into play.

Serializers:

We developed our models in the previous section. In this section, we will develop serializers. Let’s understand what are these serializers used for.

Serializers are used to transform our model instances or objects into JSON.

An API’s end result is always JSON. API’s communicate with multiple technologies which take JSON as their Input. JSON stands for JavaScript Object Notation. It is simply a JavaScript notation.

You can say it’s similar to the Python dictionary. It’s just a file that is sent/ received by the API to various applications. That’s what serializers help us do here.

Serializers are an important part of DRF. We will be using serializers to get the JSON files for our models. It’s not easy to implement with just Django. Serializers can easily create JSON responses from our model instances. This is one of the magical things DRF can perform.

To implement serializers in our project, create a new file in the lms directory called serializers.py.

Serializers.py: -

Now, we will also need views and urls to see how all this works.

Views.py: -

Urls.py: -

Now, we can run our server.

Output: -

 

0
Like