Creating a Hospital Appointment Management System with Django

johnthuo
5 min readJul 5, 2022

--

In this tutorial, I will walk you through Part 1 of creating a hospital Management system that will allow Patients to view services offered by a given hospital, their doctors, and different appointment times and let patients book/view an appointment with a given doctor. This tutorial will continue from where we left off in the previous article- Django Migrations and Projects.

The system uses Africa's Talking U.S.S.D(Unstructured Supplementary Service Data) API to enable seamless Patient-Doctor appointment booking. Once a patient makes a successful appointment, a 6-digit alphanumeric booking code generated by the system is sent to the patient's phone using an SMS shortcode. This booking code verifies the patient's appointment at the hospital.

Let's dive in

Django Project and Apps

If you remember correctly, we created a Django project to get us started in our last article. The project we created, in this case, is our entire application in Django. The different parts of the application are divided into various apps. To explain this further, I will use what we are trying to build. Our project, in this case, is 'djangohospitalappointment.' It will contain the logic that governs the whole web application. We then create various applications such as users and the hospital. Each application will have its behavior. The apps will hold our data models, views, and templates, to name a few.

Creating the Project
To create the project in Django, we use the following command:

>django-admin startproject djangohospitalappointment .

One thing, make sure you have activated your project's virtual environment before creating the project.

The project folder generated contains a settings.py which holds all the project's configurations such as database configurations, secret key, rest framework renders, static folder configs, and base dir. Later, we will learn how to hide our database configurations, Django secret key, and debug variable using python decouple.
Once we have done that, we will create our project's apps named above.
To do so, run the following command on your project's terminal:
>django-admin startapp hospital
>django-admin startapp users

Project Data Models

Inside our apps, we will create different data models that interact with each other in the project. These models include Patients, Doctors, Services, DoctorTimeSlots, Appointments, DoctorServices e.t.c In Django, a model holds the fields of a particular data object. A doctor is an example of a database model which contains fields such as name, phone number, and location.

In Django, an ORM(object-relational mapper) is used to, as from the name, map object attributes to respective table fields. The ORM's primary goal is to transmit data between a relational database and an application model. The ORM automates this transmission, so the developer does not need to write any SQL. However, it is optional and has cons, especially when dealing with a complex system.

Django also has a built-in user authentication system that handles user accounts, groups, permissions, and cookie-based user sessions. The system contains a User model whose fields include username, first_name, last_name, email, is_staff, and date_joined. We will inherit from this when creating our Patients and Doctors models.

Before using it, we need to register the User model into our database file. We do this by simply applying migrations. This results in the creation of a migration file in our various applications' migration folders, as shown below:

migrations/0001_initial.py

Command to make migration in Django:
>python manage.py makemigrations
>python manage.py migrate

In our users' application, navigate to our models.py and create the following data models; Patients and Doctors as shown :

DoctorsPatientsmodels

We will then register these models into our Project Database schema. Django's default Database is sqlite3, specified in our project's settings.py file. You can always choose another, whether myphp-based, MongoDB, or PostgreSQL. For this project, we will use sqlite3.

After applying migrations, navigate to the hospital app, models.py file, and create the following models : Services(name , description), DoctorServices(service, doctor), DoctorTimeSlots(doctor_service, start_date, end_date), and Appointments(doctor_time_slots, patient, booking_code). The fields for the named models above are enclosed within the brackets. I have provided a link to the Github project repository toward the end of the article.

Once done, navigate to your users' app, admin.py, and register your users' models(Patients, Doctors) there. Do the same to the hospital/models.py.
The Django admin is an automatically-generated user interface for Django models. The register function adds models to the Django admin so that data for those models can be created, deleted, updated, and queried through the user interface.

admin.py

To navigate to the admin side of our project, do the following:
i)Stop your server, if it was running
> press Ctrl + C
ii) Create a superuser
>python manage.py createsuperuser
iii) Enter their username, email, and password
iv) Rerun, the server
>python manage.py runserver 3000
v) Once the server is up and running, extend the localhost URL by adding admin at the end
e.g localhost:3000/admin

If done correctly, you will see the following:

admin dashboard

My admin dashboard has been customized to fit this project's look and feel. I used jazzmin to do the customization. Find within the hospital/admin.py and users/admin.py of the Project repository the customization.

Next up, we will talk about the different hospital management system views, which in a nutshell, take in HTTP requests and return HTTP responses. In the meantime, go through Django model relationships articles to understand the logic behind the relationships outlined in this project's models.

Github Repository Link :

https://github.com/john-thuo1/hospitalbookingmanagementsystem.git

--

--

johnthuo

Exploring Machine Learning, Data Engineering & IOT.