Share This Article
Django Rest Framework lets you create RESTful APIs: A way to transfer information between an interface and a database in a simple way.
- Django REST framework (DRF) is an open-source, mature, and well-supported Python/Django library.
- It aims at building web Rest APIs.
- 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 [email protected] –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.
- 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.
- 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.
- 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:
- Basic Authentication
- Token-Based Authentication
- 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:
- Function-based Views
- Class Based Views
We will explore different ways to engender a Django Rest Framework(DRF) API in a 3 part series:
- Plain APIView
- Generic APIView
- 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:
- Create Serializer
- Create Models
- Create Views
- Register Url Patterns
- Attach Different Rest APIs with URLs
- You can create API for login authentication.