Building an Open Source Python Web API Using Django REST Framework (Part Two)

Written by strivecode | Published 2023/03/07
Tech Story Tags: web-development | python-web-development | django-applications | python-programming | rest-api | databases | software-development | python

TLDRPart one of the tutorial was about the name of the API, the type of database management system chosen, and why we chose those tools. The second part will describe the API and illustrate all the entities and attributes of our database and how to set up python REST API. We conclude this part by setting up our Django project in a virtual environment.via the TL;DR App

Welcome to the second part of building an open-source python web API using the Django framework. Part one of the tutorial focused on the name of the API, the type of database management system chosen, the framework of choice for the project, and why we chose those tools.

The second part of the tutorial will describe the API and illustrate all the entities and attributes of our database, as well as how to set up python REST API. We will also convert entities and attributes to tables and fields and conclude this part by setting up our Django project in a virtual environment.

Planning a web application

Before we jump into coding, let us take a moment to think about the structure of our project. I will advise you not to skip this section because brainstorming, planning, and using diagrams for our project will drastically reduce the time spent writing our code logic for the python REST web API.

There are no strict rules on how to plan your work, but scribbling your ideas in a notebook, writing and organizing your requirements, and even generating pseudocode will save you a ton of work during the coding face of your work.

During the planning of the python REST web API, different kinds of rules in brainstorming and formulating the various requirements were used. The diagram below illustrates the entity relational diagram of the initial iteration of the structure of the python REST Tribal API. As we develop the application, we will continue to fine-tune the requirements of the application. I indicated that we are building the application from the ground up. This experience will give us a glimpse of what happens on real projects.

Tribal API ER Diagram

The relationship between the tables of the python REST web API

The python REST web API will contain the relationship that exists between tables in the database of our open-source python REST web API. The following are the relationships between our tables in the first iteration of our python REST web API using the Django REST API framework.

Tribes - Regions:

Each tribe is assigned to a distinct territory based on its location. Several tribes may coexist in a region.

Tribes - Demographics; each tribe has a distinct demographic makeup. Demographic data may differ between tribes.

Regions - Countries;each region is assigned to a single country. Countries can have many regions.

Economic Activities - Countries; all economic activities are linked to one or more countries. Economic activity may differ depending on a country's GDP and industry.

Cultural Traditions - Tribes; every tribe has its own culture and traditions. Depending on customs, religion, and behaviors, tribal cultural traditions may differ.

Environment - Regions; every region has its own set of environmental elements. Climate, natural resources, and pollution may cause environmental challenges to differ between locations.

Health and Wellness - Tribes; each tribe has its health and wellness customs. Depending on diet, healthcare systems, exercise, health, and well-being may differ amongst tribes.

Traditional Authority - Country

Each country has its own set of traditional authorities. Traditional authority may differ amongst countries depending on the form of government, human rights, and foreign ties.

Demographics - Health and Wellness:

Each demographic group has its own set of health and wellness behaviors. Age, gender, ethnicity, and education level all impact health and wellness.

Setting up the web API on a virtual machine

I am using Ubuntu Jammy, you should do some research and figure out how to set up a virtual environment on your operating system. A virtual environment helps to isolate our web API dependencies from interacting with global python modules and packages.

# Create the project directory
mkdir tribal_project
cd tribal_project

# Create a virtual environment
python3 -m venv env
source env/bin/activate

Look for how to create a virtual environment on your specific operating system. Create a Django REST framework app, tribal_project, in your virtual environment and start a new REST API application called tribal_app, shown below.

# Install Django and Django REST framework into the virtual environment
pip install django
pip install djangorestframework

# You may install all at once
pip install django djangorestframework

# Set up a new project with a single application
django-admin startproject tribal_project .  # Note the period at the end of the command

cd tribal_project # if you did not add the period at the end of the command

# Install the tribal_app
django-admin startapp tribal_app

Open the IDE or code editor of your choice. I am using Visual Studio Code. Make sure to set the python development environment on visual studio code. Whilst at the project root directory run the code below.

code . #Take of the period

Everything went well you should have this type of file arrangement in your vs code explorer.

Also, install the following extension for a breezy development experience using python on vs code. Python, Pylance, Lightrun, and Black formatter.

Part three of our work will go straight into the coding of the web application. Bracee yourself for a long ride. Once again, we never stop learning. Good luck and see you in the next series.


Written by strivecode | I love technology and innovation, web development and problem-solving. I am building apps and technical writing them!
Published by HackerNoon on 2023/03/07