Getting Started

  1. Make sure you installed django and isolate it in virtual environment. For this article, we use the following code to create directory and virtual environment.

..\\tutorial> python -m venv venv

Code above said that we created .env virtual environment inside the tutorial folder. Then, activate the virtual environment using :

..\\tutorial> cd venv ..\\tutorial\\venv> Scripts\\activate

If you run code above successfully, you will find the following command on the terminal, and the virtual environment is ready to go

(venv) ..\\tutorial\\venv> 2. If you are using VSCode, make sure you change the interpreter on current virtual environment with command palette or by press Ctrl + Shift + F. You can find the command palette at View tab.

![<https://s3-us-west-2.amazonaws.com/secure.notion-static.com/fcf072d6-5eb0-4ba4-9957-7debb2c0ca56/gini.png>](<https://s3-us-west-2.amazonaws.com/secure.notion-static.com/fcf072d6-5eb0-4ba4-9957-7debb2c0ca56/gini.png>)

Choose the virtual environment at current folder as interpreter, on this article, the `venv` virtual environment

![gini.png](<https://s3-us-west-2.amazonaws.com/secure.notion-static.com/5160a959-7365-41b7-992a-9c5e0d330ad4/gini.png>)
  1. Install django and django restframework using

(venv) ..\\tutorial> pip install django (venv) ..\\tutorial> pip install djangorestframework 4. Set up a new django project by using

(venv) ..\\tutorial> django-admin startproject tutorial .

Create the application by typing

(venv) ..\\tutorial> python manage.py startapp api 5. Here is the following folder structure for getting started our django restframework

gini.png

Application Setup

Before we code our django restframework, we need to register the django restframework and the application. To register, head into settings.py file inside django folder (on this article, django_restframework refers to django folder), scroll down until you find INSTALLED_APPS list, register the django REST framework and the application.

gini.png

The 'library.apps.LibraryConfig' value basically refers to api folder → apps.py file → ApiConfig class.

gini.png

We are done with initial project setup, now we are heading for define the model/database

Defining Model

In Django, we define the database with ORM or Object Relational Mapper. In short, ORM wraps how we define table and fetch the record. Here, we define model/table at models.py file which located at api folder. In code below, we define DailyCases model using python class.

from django.db import models

# Create your models here.
class DailyCases(models.Model):
    date = models.DateField(blank=False, null=False, unique=True)
    cummulative = models.IntegerField(default=0)
    recoverd = models.IntegerField(default=0)
    under_treatment = models.IntegerField(default=0)
    deaths = models.IntegerField(default=0)

    def __str__(self):
        return str(self.date)

After we define the model, we should register it on admin.py file. The file is located within api folder. To register the model, we use code below.

from django.contrib import admin
from .models import DailyCases

# Register your models here.
admin.site.register(DailyCases)

Next, we should make migrations and migrate to commit changes on our database. We type the following command on terminal/console.

(venv) ..\\tutorial> python manage.py migrate (venv) ..\\tutorial> python manage.py makemigrations