Skip to content
Django
10. Django Rest Framework

Django Rest Framework

Introduction to API

1.1. What is API?

An API (Application Programming Interface) is a software intermediary that allows two or more applications to talk to each other.

API Types: in terms of release polcies:-

  1. Private APIs: Internal APIs used by a company to build its own products.
  2. Public APIs: External APIs used by a company's customers to access its services.
  3. Partner APIs: APIs exposed by one company to another company's developers.

Example 1: Imagine you're at a hotel and you decide to order food. You summon the waiter and relay your order to them. The waiter then conveys your order to the kitchen and subsequently delivers the prepared food to you. In this scenario, the waiter serves as an interface (API) bridging the communication between you and the kitchen.

Example 2: Imagine you're at a hotel and you decide to order food. You summon the waiter and relay your order to them. The waiter then conveys your order to the kitchen and subsequently delivers the prepared food to you. In this scenario, the waiter serves as an interface (API) bridging the communication between you and the kitchen.

API

Example 3: Suppose you've built an application using a particular framework and you want to incorporate Google Map services. In this case, you would utilize Google's API (the provider) to integrate these services into your application (the consumer).

Use Case

  • Suppose you want to display a variety of products from diverse companies such as Amazon, Flipkart, JioMart, and Snapdeal on your website.
  • You aim to display this data across multiple platforms and intend to write your source code in different programming languages.

API

How to use API

For a particular user there is an token authentication has provided by API provider from their documentation. We have to use that token in our request header to get the data from API.

API

Django REST Framework

Django REST Framework is a powerful and flexible toolkit for building Web APIs.

  • Web browsable API is a huge usability win for your developers.
  • Authentication policies including packages for OAuth1 and OAuth2.
  • Serialization that supports both ORM and non-ORM data sources.
  • Customizable all the way down - just use regular function-based views if you don’t need the more powerful features.
  • Extensive Documentation, and great community support.
  • Used and trusted by internationally recognized companies including Mozilla, Red Hat, Heroku, and Eventbrite.

2.1. Installation

Install using pip command:

pip install djangorestframework
INSTALLED_APPS = [
    ...
    'rest_framework',
]
# python version
python --version
 
# django version
dajgno-admin --version
 
# install DRF
pip install djangorestframework

Other packages:

pip install markdown       # Markdown support for the browsable API.
pip install django-filter  # Filtering support
pip install django-guardian # Object level permissions support
pip intall pyyaml          # YAML content type support.
pip install pygments       # Add syntax highlighting to Markdown processing.

2.2. Serialization

Serialization is the process of converting complex data such as querysets and model instances to native Python datatypes that can then be easily rendered into JSON, XML or other content types. Deserialization is the opposite process of decoding data into complex types.

Python JSON

It is easy to convert a Python data structure to JSON using the json module. This can be done using the json.dumps() function.

json.loads() function takes a JSON string and converts it back to a Python data structure.

import json
 
data = {
    'name': 'ACME',
    'shares': 100,
    'price': 542.23
}
 
# Python data structure to JSON
json_str = json.dumps(data)
# JSON string to Python data structure
data = json.loads(json_str)