Django Rest Framework

This past week I started a tutorial to learn the Django REST Framework. My main goal was to discover what the Django REST Framework offers on top of Django

Introduction

REST APIs are an industry-standard way for web services to send and receive data. They use HTTP request methods to facilitate the request-response cycle and typically transfer data using JSON, and more rarely – HTML, XML and other formats.

What is a REST API?

REST (Representational State Transfer) is a standard architecture for building and communicating with web services. It typically mandates resources on the web are represented in a text format (like JSON, HTML, or XML) and can be accessed or modified by a predetermined set of operations. Given that we typically build REST APIs to leverage with HTTP instead of other protocols, these operations correspond to HTTP methods like GET, POST, or PUT.

An API (Application Programming Interface), as the name suggests, is an interface that defines the interaction between different software components. Web APIs define what requests can be made to a component (for example, an endpoint to get a list of shopping cart items), how to make them (for example, a GET request), and their expected responses.

What is the Django-REST framework?

The Django REST Framework (DRF) is a package built on top of Django to create web APIs. One of the most remarkable features of Django is its Object Relational Mapper (ORM) which facilitates interaction with the database in a Pythonic way.

You can create classic web applications via Django and expose their functionality to the world through REST APIs. In fact, this is pretty easy to do! Though, the Django REST Framework is more specialized for this task, is built on top of plain Django and makes the process easier.

Requirements for django-REST framework.

  • python version 3.6+ to latest
  • django version 2.2 ,3.0,3.2,4.0 etc

Why Django-REST framework?

The biggest reason to use Django REST Framework is that it makes serialization so easy!

Creating Django-REST API .

We will be using Django version  4.0.3

Setting up Django project

 

 

Once the project is created we dive into the structure of DJANGO

On top level we have root directory which have our project and the project consist of our app and manage.py file , we found some pre installed python files inside our directory

Each django project comes with pre-installed few django application , we have to list our api_app inside INSTALLED_APPS list.

list it by going to setting .py file

 

 

 

 

Installing REST-API

 

 

 

Serializers

Serializers define the representation of our model in JSON format and convert object instances to a more transferable format. This will simplify the parsing of data for our API. Deserializers do the opposite – they convert JSON data into our models as object instances.

 

 

 

 

 

 

Leave a Comment