A comprehensive REST API-based hospital management system built with Django REST Framework.
This system manages hospitals, departments, administrators, doctors, patients, medicines, and diseases through a set of interconnected REST APIs.
- Create and manage multiple hospitals
- Track hospital details including name, address
- View all hospitals in the system
- Create departments within hospitals
- Assign administrators to departments
- Track department-specific information
- Administrators can manage multiple departments
- Each administrator is associated with a specific hospital
- Track administrator details and permissions
- Manage doctors across different departments
- Track doctor specializations and status
- Associate doctors with specific hospitals and departments
- Track patient visits and medical history
- Associate patients with doctors
- Manage patient status (admitted/discharged/waiting)
- Record treatments and medications
- Maintain medicine database
- Track diseases and their treatments
- Associate medicines with patients
GET /hospitals/- List all hospitalsPOST /hospitals/- Create new hospitalGET /hospitals/{id}/- Get hospital detailsPUT /hospitals/{id}/- Update hospitalDELETE /hospitals/{id}/- Delete hospital
GET /departments/- List all departmentsPOST /departments/- Create new departmentGET /departments/{id}/- Get department detailsPUT /departments/{id}/- Update departmentDELETE /departments/{id}/- Delete department
GET /administrators/- List all administratorsPOST /administrators/- Create new administratorGET /administrators/{id}/- Get administrator detailsPUT /administrators/{id}/- Update administratorDELETE /administrators/{id}/- Delete administrator
GET /doctors/- List all doctorsPOST /doctors/- Create new doctorGET /doctors/{id}/- Get doctor detailsPUT /doctors/{id}/- Update doctorDELETE /doctors/{id}/- Delete doctor
GET /patients/- List all patientsPOST /patients/- Create new patientGET /patients/{id}/- Get patient detailsPUT /patients/{id}/- Update patientDELETE /patients/{id}/- Delete patient
GET /medicines/- List all medicinesPOST /medicines/- Create new medicineGET /medicines/{medicine_id}/- Get medicine detailsPUT /medicines/{medicine_id}/- Update medicineDELETE /medicines/{medicine_id}/- Delete medicine
GET /diseases/- List all diseasesPOST /diseases/- Create new diseaseGET /diseases/{disease_id}/- Get disease detailsPUT /diseases/{disease_id}/- Update diseaseDELETE /diseases/{disease_id}/- Delete disease
GET /api/patient-status/?name={name}- Get current status of a patientGET /api/patient-visits/?name={name}&start_date={date}&end_date={date}- Get patient visit history
- Django
- Django REST Framework
- MySQL Database
- Python 3.x
- Clone the repository:
git clone https://github.com/praths71018/django_and_rest_framework_fundamentals_HospitalManagement.git- Create virtual environment:
python -m venv venv- Activate virtual environment:
source venv/bin/activate # On Windows: venv\Scripts\activate-
Create a folder src in same directory as virtual environment and add all the cloned files inside it.
-
Run migrations:
python manage.py migrate- Run the server:
python manage.py runserver- Install MySQL dependencies:
pip install mysqlclient- Create MySQL database:
CREATE DATABASE HospitalShadowfax;
USE HospitalShadowfax;- Update settings.py with MySQL configuration:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'HospitalShadowfax',
'USER': 'root',
'PASSWORD': 'your_password',
'HOST': 'localhost',
'PORT': '3306',
}
}-
Install MySQL Server:
- Windows: Download and install MySQL Server from MySQL Official Website
- Ubuntu/Debian:
sudo apt update sudo apt install mysql-server sudo mysql_secure_installation
- macOS:
brew install mysql brew services start mysql
-
Apply migrations:
python manage.py makemigrations
python manage.py migrate- Verify connection:
python manage.py dbshellSHOW TABLES;
select * from hospitals_hospital;
select * from departments_department;
select * from administrator_administrator;
select * from doctors_doctor;
select * from medicine_medicine;
select * from diseases_disease;
select * from patients_patient;The system uses the following models:
- Hospital
- Department
- Administrator
- Doctor
- Patient
- Medicine
- Disease
Each model maintains appropriate relationships with other models through foreign keys and many-to-many relationships.
- URL: https://www.youtube.com/watch?v=k7pf42xD4bw
- Define a test class in the test file
- Define a test method in the test class
- Use the assert statement to check the expected result
- Run the test using the test command
python manage.py test hospitals- URL: https://www.youtube.com/watch?v=a6zVm5UqOoo
- Signals are used to send notifications or perform actions when certain events occur in the database
- Use the
post_savesignal to send a notification when a model is saved - Use the
post_deletesignal to send a notification when a model is deleted - Use the
pre_savesignal to send a notification when a model is saved and see previous data - Use the
pre_deletesignal to send a notification when a model is deleted and see previous data - Modify the models.py file to add signals
- Template : in models.py file
@receiver(pre_save, sender=Medicine)
def send_medicine_notification(sender, instance, **kwargs):
if instance.medicine_id: # Check if the medicine already exists (not a new instance)
previous_instance = sender.objects.get(medicine_id=instance.medicine_id)
print(f"Medicine updated: {instance.medicine_name}")
print(f"Previous data: {previous_instance.medicine_name}")-
Token Authentication is used to authenticate the user
-
In settings.py file, add the following:
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': [
'rest_framework.authentication.TokenAuthentication',
]
}- Make an app for authentication
- Authentication app contains login and signup views.
- It uses a User model to store user details. Predefined user model is used provided by django.
- Define a serializer for the User model.
- Define a view for the login view.
- Define a view for the signup view.
- Define urls.py file for the authentication app.
- Then in administrator app, add the following to routes/urls you want to protect/authenticate at beginning of class:
from rest_framework.permissions import IsAuthenticated
class YourClass(APIView):
permission_classes = [IsAuthenticated]- In settings.py file, add the following:
INSTALLED_APPS = [
...
'rest_framework.authtoken',
...
]- Now go to url which is authenicated:
- It tells you to login
- Login first.
- It gives you a token
- Open postman and go to url which is authenicated
- Go to headers tab
- Copy the token and paste it in the authorization header in postman
- Key: Authorization
- Value: Token
- Send request
- It will give you the response
- URL: https://www.youtube.com/watch?v=JYQG7zlLJrE&t=445s
- Celery is used to send messages and tasks asynchronously.
- It acts as a message broker between the application and the worker.
- Decrease the response time of the application
- Allows to perform tasks in the background
- Example: sending email to the patient when the patient is admitted
- Install celery:
pip install celery- Install redis: (redis is used as the message broker)
brew install redis- Run redis server:
redis-server-
pip install redis
-
Configure celery:
-
In system create a file called celery.py
-
Add the following code:
import os
from celery import Celery
os.environ.setdefault('DJANGO_SETTINGS_MODULE', '<core>.settings')
app = Celery('<core>')
app.config_from_object('django.conf:settings', namespace='CELERY')
app.autodiscover_tasks()- In settings.py file, add the following:
CELERY_BROKER_URL = 'redis://localhost:6379'
CELERY_RESULT_BACKEND = 'redis://localhost:6379'- Add below code in init.py file of the app where you want to use celery:
from .celery import app as celery_app
__all__ = ('celery_app',)- Create a file called tasks.py in the app where you want to use celery (patients):
from celery import shared_task
from .models import Patient
@shared_task
def send_patient_email(email, message):
print(f"Sending email to {email} with message: {message}")- Then in views.py file, add the following code:
from .tasks import send_patient_email # Import the task to send email
class PatientListView(APIView):
def post(self, request):
data = request.data
patient = Patient.objects.create(**data)
send_patient_email.delay(patient.patient_id) # Send email asynchronously
return Response({"message": "Patient created and email sent"}, status=status.HTTP_201_CREATED)-
Add App password in settings.py file :
- First go to google account settings and create app password (2 step verification should be enabled)
- Then add the app password in settings.py file
- Then add the email address in settings.py file
-
Run celery worker:
celery -A system worker -l info- URL: https://www.youtube.com/watch?v=A4PAJDkHJfI
- Middleware is used to process requests and responses
- Before a request is processed by views.py file , it is processed by middleware.
- Example: If authentication is enabled for the api/class , before it is processed by views.py file , it is processed by middleware. ('django.contrib.auth.middlewareAuthenticationMiddleware', in settings.py file)
- Middleware is used to add custom headers to the request and response
- Middleware is called based on sequence in settings.py file i.e. in :
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]First security middleware is called , then session middleware is called , then common middleware is called , then csrf middleware is called , then authentication middleware is called and so on. Hence order is important.
- To create a middleware, create a folder called middleware in the app where you want to use it.
- Create a init.py file in the middleware folder.
- Create a main.py file in the middleware folder and add functions to it you want to execute before the request is processed by views.py file by any api/class.
- Add the middleware in settings.py file in MIDDLEWARE list.
- If you want to add middleware in only one api/class, add it in the class you want to protect as middleware.py file.
- URL: https://www.youtube.com/watch?v=JYQG7zlLJrE&t=445s
- Celery Beat is used to schedule tasks to run at a specific time.
- pip install django-celery-beat
- pip install django-celery-results
- Add the following in settings.py file:
INSTALLED_APPS = [
...
'django_celery_results',
'django_celery_beat',
...
]- In tasks.py file, create a function to send emails to all patients at a specific time.
- In celery.py file, add the following code:
app.conf.beat_schedule = {
'send-patient-details-every-day': {
'task': 'patients.tasks.send_all_patients_email',
'schedule': crontab(hour=14, minute=20),
},
}- Run the server:
python manage.py runserver- Run celery worker:
celery -A system worker -l info- Run celery beat:
celery -A system beat -l info- URL: https://www.youtube.com/watch?v=AKa7VTZwLzQ
- pip install cryptography
- Create a file called encryption.py in the utils folder in the patients app
- Create encrypt and decrypt functions in the encryption.py file
- Open the terminal and run the following command to generate a key:
python manage.py shell- Add the following code in the shell:
from cryptography.fernet import Fernet
key = Fernet.generate_key()
print(key)- Copy the key and paste it in the ENCRYPT_KEY variable in the settings.py file
- In models.py file, add the following code:
from .utils.encryption import encrypt, decrypt # Import the encrypt and decrypt functions
def save(self, *args, **kwargs):
# Encrypt the phone number before saving
if self.phone:
self.phone = encrypt(self.phone)
super().save(*args, **kwargs)
def get_decrypted_phone(self):
# Decrypt the phone number when retrieving
return decrypt(self.phone) if self.phone else None- Encrypt the phone number before saving and decrypt the phone number when retrieving in the views.py file
- Create a New Relic account
- Go to APM and Services and create a new application
- Copy the license key
- Download the New Relic Python Agent in the directory where the project is located
- Create a newrelic.ini file in the directory where the project is located (downloaded from newrelic)
- pip install newrelic
- In terminal run the following command:
newrelic-admin generate-config YOUR_NEW_RELIC_LICENSE_KEY newrelic.ini- Modify the wsgi.py file to include the newrelic.ini file
import newrelic.agent
newrelic.agent.initialize('/Users/prathamshetty/Desktop/Shadowfax/Hospital/newrelic.ini') # Update the path to your newrelic.ini file- Run the server
gunicorn system.wsgi:application- Go to New Relic and see the application
-
Goto the project directory
-
git init
-
git branch -m main
-
git remote add origin https://prathamshetty2@bitbucket.org/hospital_management_pratham/hospital_management.git
-
git add --all -
git commit -m "Initial commit" -
Before pushing to bitbucket Go to personalised bitbucket settings and add app password for the repository
-
generates password for the repository
-
git push -u origin main
-
git add --all
-
git commit -m "commit message" -
git push
- create a new branch:
git checkout -b <branch_name>- push the branch to bitbucket:
git push -u origin <branch_name>- merge the branch to main:
git checkout main
git merge <branch_name>
git push origin main- delete the branch:
git branch -d <branch_name>- Pull request to main branch:
- Go to bitbucket and create a pull request from the branch to main
- Merge the pull request
- Delete the branch