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.
Django rest framework

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 [email protected] –username admin

Set up your Django REST Framework API


#code omitted for brevity




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.
You May Also Like