Django REST Framework

Django REST Framework – ProgramSpeaker

Django REST framework(DRF) is a powerful and flexible toolkit for building Web APIs. (Official documentation) Django REST framework is a tool in the backend category of tech stack. Which is an open-source, mature, and well-sophisticated Python Django library for building web APIs. Before moving just take a look into Django and REST API.

What is Django?

Django is a free, open-source, and powerful python based framework for building web applications by following the MVT (Model View Template)architecture. It is maintained by the Django software foundation, an independent established in the US as a 501 non-profit. stable release – 4.0.5 (August 2022)

You may like: What is Django and Why Django? An Introduction to Django

REST API?

REST API is a software architectural pattern developed by Roy Fielding in 2000 for creating web services. It is a web service API that user URLs, HTTP protocol, and JSON for data format. GET, POST, PUT, PATCH, and DELETE are the HTTP methods used by REST applications. REST API is one of the most popular types of APIs. The use of REST APIs has grown exponentially over the years and now also. Some of the major benefits of REST APIs are lightweight, Independent, scalable, and flexible.

You may like: What is the difference between API and REST API

Django REST framework?

Django REST framework(DRF) is a flexible and fully featured toolkit with modular and customizable architecture and it makes the development of both simple, turn-key API endpoints. the developers to make an application rapidly. So Django is meant for Rapid Application Development(RAD) projects. Latest version: 3.13.1 (August 2022)

One of the most remarkable features of Django is its Object Relational Mapper(ORM) which facilitates interaction with the database in a pythonic way. However, we can not send python objects directly over a network. For a solution, we use a serializer to translate Django models into some other formats like JSON, XML, and vice-versa. This is sometimes a challenging process this is made easier by the Django REST framework.

Installation

Install using pip(install now).

pip install djangorestframework

Add ‘rest_framework’ to your INSTALLED_APPS settings.py

INSTALLED_APPS = [
…
'rest_framework'
]

RESTful Structure

In a RESTful API, endpoints(URL) define the structure of the API. GET, POST, PUT/PATCH, and DELETE are the HTTP methods used to access the data from our application.

  • POST: This is an HTTP method used to create a new entry.
  • GET: The most common HTTP method is GET. which returns a representational view of data and is used in read-only mode, which keeps the data safe.
  • PUT: Using the PUT method we can update or replace the existing resource, which updates resources by replacing its content entirely.
  • PATCH: PATCH is another HTTP method used to update resources. Which modifies only the required resources not the entire content like the PUT method.
  • DELETE: The last HTTP method is DELETE. Using this method we can remove a resource entirely.

Serialization

Serializers allow complex data such as query sets and model instances to be converted to native python data types that can be easily rendered into JSON and XML or other content types. Serializers also provide de-serialization allowing parsed data to be converted back into completed types, after first validating the incoming data (Official documentation)

In Django rest framework sterilizers is responsible for converting objects into data types understandable by frond end framework and javascript. Serializers also provide de-serialization allowing parsed data to be converted back into complex types after validating the incoming data. Serializers are also used to validate in the Django rest framework.

Let us take an example of creating a serializer(serializer.py).

from rest_framework import serializers
from rest_framework.serializers import ModelSerializer
from .models import YourModel

class YourSerilalizerName(serializers.ModelSerializer):
    class Meta:
        model = YourModel
        fields = '__all__' # List all fields from YourModel

In the End

Django REST framework(DRF) is a package built on top of Django to create web APIs. Here we discussed a minimal idea of DRF. In this tutorial, we move through the installation, serialization, and the basic restful structure (post(), get(), put()/patch(), and delete()).

Share this:

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top