Skip to main content

Making API

Let's create a web app which exposes API like create, get, list, update and delete on inventory items using rest API calls.
Before starting with this tutorial I assume that you have your project setup done, if not then you can refer to the settings page.
Now as you have your project setup done, let's install Django-rest-framework in our virtual environment.
To install Django-rest-framework run following command

pip install djangorestframework



After you have successfully install django-rest-framework, you have to tell Django that you have installed django-rest-framework. For this, you have to make entry of djangorestframework in variable INSTALLED_APPS which is in settings.py


INSTALLED_APPS = (
    ...
    'rest_framework',
)

Now Django is aware that rest_framework is installed.
Let's create an app where we will write code for our inventory system. Run the following command to create an app.


python manage.py startapp api

Now as we have created API app we need to make Django aware that we have created a new app. For this, we have to make entry of API in variable INSTALLED_APPS which is settings.py
Let's first create a model class for our inventory system, open up api/models.py
Now create a class Inventory which will have some fields like name, price, quantity


class Inventory(models.Model):
  name = models.CharField(max_length=100, default="")
  price = models.IntegerField(default=0)
  quantity = models.IntegerField(default=0)

now run commands
python manage.py makemigrations  # to let django know what model changes we have made

python manage.py migrate # to let django make changes in database
So up till now, we have created our model for inventory system

Now let's create a file named as serializers.py under api package. This file will hold all serializer class for the inventory models
Open serializers.py here we need to write model serializer for inventory model
as given below

from rest_framework import serializers

from .models import Inventory


class InventorySerializer(serializers.ModelSerializer):
  class Meta:
    model = Inventory
    fields = '__all__'
Now we need to write view for our api which will expose curd operations on inventory
Open api/views.py
we will be writing modelviewset which is provided by djangorestframework. It will reduce code
as it takes care of all curd operations itself

first, we need to import modelviewset, we can import it using the following statement



form rest_framework.viewset import ModelViewSet

Now we need to make our class InventoryModelViewSet that will inheret ModelViewSet

from rest_framework.viewsets import ModelViewSet

from .serializers import Inventory, InventorySerializer


class InventoryModelSerializer(ModelViewSet):
  queryset = Inventory.objects.all()
  serializer_class = InventorySerializer
In this class, variable queryset tells models viewset that what query to fire by default.
serializer_class tells that which serializer will take care of validations, serialization, and deserialization

Now we need to bind this view to URL

from api.views import InventoryModelViewSet
from django.contrib import admin
from django.urls import path
from rest_framework.routers import SimpleRouter

router = SimpleRouter()

router.register('inventory', InventoryModelViewSet, base_name='inventory')
urlpatterns = [
  path('admin/', admin.site.urls),

]

urlpatterns += router.urls


python manage.py runserver



Now open a browser and navigate to inventory endpoint as shown


Here is the link to download source code. download

Comments