Python

What is Django Rest Framework?

Django Rest Framework lets you create RESTful APIs: A way to transfer information between an interface and a database in a simple way.

  1. Django REST framework (DRF) is an open-source, mature, and well-supported Python/Django library.
  1. It aims at building web Rest APIs.
  1. The Django REST framework contains a wide set of out-of-the-box features, but the core view class is very simple and the framework in general is easy to use.

Steps to start with Django rest framework

Setting Up environment:

python3 -m venv env

source env/bin/activate

(On Windows, run source env\Scripts\activate instead.)

Install Django and Django Rest Framework:

django-admin start project blog

cd blog

django-admin startapp api

Run Migrations:

python manage.py makemigrations

python manage.py migrate

Create SuperUser(Admin):

A. python manage.py createsuperuser –email admin@example.com –username admin

Set up your Django REST Framework API

INSTALLED_APPS[

#code omitted for brevity

‘rest_framework’,

‘api.apps.ApiConfig’,

]

Start the local development server:

python manage.py run server

These are the basic things we can do to start a Django rest framework project.

Why choose Django Rest Framework for Development?

One of the main reasons for using the Django rest framework is that it makes serialization so easy and fast.

  1. Here APIs play a major role, if you create APIs of the common business logic, then you just need to access those APIs using URLs in your different applications.
  1. The Django REST Framework, then, plays nicely with the Django ORM that makes handling database queries easy. With a few lines of code you can serialize your database models to REST-ful formats.
  1. The main idea behind the DRF is to clearly divide a model, the generalized wire representation (e.g. JSON, XML, etc.), and set of generic Class-Based-Views that can be customized to satisfy the specific API endpoint using Serializer that describes the mapping between them.

Django Rest Framework Authentication

Authentication is the mechanism of associating an incoming request with a set of identifying credentials, such as the utilizer the request emanated from, or the token that it was signed with.

The sanction and throttling policies can then utilize those credentials to determine if the request should be sanctioned

Types of Authentication: 

  1. Basic Authentication
  2. Token-Based Authentication
  3. 3rd Party Packages

Django Rest Framework APIView

Django’s views are a welcome departure from the old-style views. we’re migrating an existing RESTful API based on function-based views to class-based views.

There are mainly 2 types of Views:

  1. Function-based Views
  2. Class Based Views

We will explore different ways to engender a Django Rest Framework(DRF) API in a 3 part series:

  1. Plain APIView
  2. Generic APIView
  3. ViewSets

Congratulations! Now you are ready to start with the Django rest framework. After reading this you are able to set up and run a Django rest framework project. After reading this you are ready to learn the following things:

  1. Create Serializer
  2. Create Models
  3. Create Views
  4. Register Url Patterns
  5. Attach Different Rest APIs with URLs
  6. You can create API for login authentication.
Aniruddh Agarwal

Founder and CEO of Extern Labs Inc. and A Proven Multi-Skill Developer. I help Startups and Businesses in building their dream projects with advanced tech. tools.

Published by
Aniruddh Agarwal

Recent Posts

Enhancing Cybersecurity in the Digital Age: Protecting Corporate Data from Emerging Threats

In today's interconnected world, businesses rely on digital platforms for almost every aspect of their… Read More

September 30, 2024

Sustainability in the Corporate World: Strategies for 2024

Corporations are increasingly expected to lead by example as the global community becomes more aware… Read More

September 19, 2024

The Future of Remote Work: Hybrid Models and Beyond

The workplace has transformed dramatically in recent years, driven by rapid technological advancements and global… Read More

September 16, 2024

Embracing AI for Business Transformation: The Future is Here

In today's fast-paced digital landscape, businesses are constantly searching for ways to stay ahead of… Read More

September 12, 2024

WordPress vs Strapi: Choosing the Right CMS for Your Next Project

In the ever-evolving landscape of website development, selecting the right Content Management System (CMS) is… Read More

February 27, 2024

Exploring the Pros and Cons of AR & VR in EduTech

Education is a realm constantly evolving with technological advancements. Augmented Reality (AR) and Virtual Reality… Read More

December 12, 2023