In this view, we first retrieve the Appointment object with the specified appointment_id. Then, if the request method is POST, we create a new Consultation object with the appointment and user input and save it to the database. Finally, we render the consultation_created.html template with the new Consultation object as context. If the request method is not POST, we render the create_consultation.html template with the appointment object as context.
To include patient medical history, consultations, and medical prescription dispensed, we have defined models for MedicalHistory, Consultation, and MedicalPrescription in the appointments/models.py file. To add data to these models, we can create forms for the user to input data and create views to process the form data and save it to the database.
To indicate whether a patient is a medical aid member or private, we have added a boolean field is_medical_aid_member to the Patient model. If is_medical_aid_member is True, the patient is a medical aid member and does not have to pay, otherwise, the patient is private and must pay cash.
To include the medical aid provider and medical aid membership number for patients who are medical aid members, we have added two additional fields to the Patient model: medical_aid_provider and medical_aid_membership_number. These fields will store the name of the medical aid provider and the membership number for the patient. We can then use this information to bill the medical aid provider directly for the patient's appointments and treatments.
User
continue
Assistant
Here is the updated models.py file with the new fields:
css
from django.db import models
from django.contrib.auth.models import User
class MedicalAidScheme(models.Model):
name = models.CharField(max_length=100)
description = models.TextField()
def __str__(self):
return self.name
class Patient(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
is_medical_aid_member = models.BooleanField(default=False)
medical_aid_provider = models.CharField(max_length=100, blank=True, null=True)
medical_aid_membership_number = models.CharField(max_length=100, blank=True, null=True)
medical_aid_scheme = models.ForeignKey(MedicalAidScheme, on_delete=models.SET_NULL, null=True, blank=True)
# other patient fields like name, date of birth, etc.
class MedicalHistory(models.Model):
patient = models.ForeignKey(Patient, on_delete=models.CASCADE)
condition = models.CharField(max_length=100)
diagnosis_date = models.DateField()
treatment_notes = models.TextField(blank=True, null=True)
class Appointment(models.Model):
patient = models.ForeignKey(Patient, on_delete=models.CASCADE)
date = models.DateField()
time = models.TimeField()
duration = models.IntegerField() # in minutes
is_confirmed = models.BooleanField(default=False)
class Consultation(models.Model):
appointment = models.ForeignKey(Appointment, on_delete=models.CASCADE)
date = models.DateField()
time = models.TimeField()
notes = models.TextField(blank=True, null=True)
class MedicalPrescription(models.Model):
consultation = models.ForeignKey(Consultation, on_delete=models.CASCADE)
medication_name = models.CharField(max_length=100)
dosage = models.CharField(max_length=50)
frequency = models.CharField(max_length=50)
duration = models.CharField(max_length=50)
User
To determine whether a patient is a: is incomplete
Assistant
To determine whether a patient is a medical aid member or private, we can use the is_medical_aid_member field on the Patient model. If this field is True, the patient is a medical aid member and should not have to pay for medical expenses. If the field is False, the patient is a private patient and will need to pay for medical expenses.
We can use this information to calculate the cost of a consultation and prescription, depending on the patient's status. For example, we can add a cost field to the Consultation and MedicalPrescription models and use a view to calculate the cost based on the patient's status:
python
# views.py
from django.shortcuts import render, redirect
from .models import Patient, MedicalHistory, Appointment, Consultation, MedicalPrescription
from .forms import MedicalHistoryForm, ConsultationForm, MedicalPrescriptionForm
def create_consultation(request, appointment_id):
appointment = Appointment.objects.get(id=appointment_id)
patient = appointment.patient
if request.method == 'POST':
form = ConsultationForm(request.POST)
if form.is_valid():
consultation = form.save(commit=False)
consultation.appointment = appointment
consultation.save()
if patient.is_medical_aid_member:
consultation.cost = 0
else:
consultation.cost = 100 # default cost for private patients
consultation.save()
return redirect('patient_detail', patient_id=patient.id)
else:
form = ConsultationForm()
return render(request, 'create_consultation.html', {'form': form, 'appointment': appointment})
In this view, we first retrieve the Appointment object with the specified appointment_id. Then, we use the patient field of the appointment to determine whether the patient is a medical aid member or private. If the patient is a medical aid member, we set the consultation cost to 0. If the patient is a private patient, we set the consultation cost to a default value of 100.
We can use a similar approach to calculate the cost of a prescription, depending on the patient's status.
User
continue
Assistant
To calculate the cost of a prescription, we can use a similar approach as we did for consultations. We can add a cost field to the MedicalPrescription model and use a view to calculate the cost based on the patient's status:
python
# views.py
def create_medical_prescription(request, consultation_id):
consultation = Consultation.objects.get(id=consultation_id)
patient = consultation.appointment.patient
if request.method == 'POST':
form = MedicalPrescriptionForm(request.POST)
if form.is_valid():
prescription = form.save(commit=False)
prescription.consultation = consultation
prescription.save()
if patient.is_medical_aid_member:
prescription.cost = 0
else:
prescription.cost = 50 # default cost for private patients
prescription.save()
return redirect('patient_detail', patient_id=patient.id)
else:
form = MedicalPrescriptionForm()
return render(request, 'create_medical_prescription.html', {'form': form, 'consultation': consultation})
In this view, we first retrieve the Consultation object with the specified consultation_id. Then, we use the patient field of the appointment associated with the consultation to determine whether the patient is a medical aid member or private. If the patient is a medical aid member, we set the prescription cost to 0. If the patient is a private patient, we set the prescription cost to a default value of 50.
We can also include the patient's medical aid provider and membership number in the Patient model, as well as a category of medical aid schemes that the patient belongs to. This information can be used to determine whether the patient's medical aid covers the cost of a consultation or prescription, and to retrieve any relevant information about the patient's medical aid coverage.
For example, we could add the following fields to the Patient model:
User
We can then pass this information to the patient_detail.html template, where we can display it: is incomplete
Assistant
Here is an example of how we could display the patient's medical aid information in the patient_detail.html template:
php
{% extends 'base.html' %}
{% block content %}
<h1>{{ patient.first_name }} {{ patient.last_name }}</h1>
<p>Email: {{ patient.email }}</p>
<p>Phone number: {{ patient.phone_number }}</p>
{% if patient.is_medical_aid_member %}
<h2>Medical Aid Information</h2>
<p>Provider: {{ medical_aid_details.Provider }}</p>
<p>Membership number: {{ medical_aid_details.Membership number }}</p>
<p>Category: {{ medical_aid_details.Category }}</p>
<!-- additional details about the patient's medical aid coverage -->
{% endif %}
<!-- display the patient's medical history, consultations, and prescriptions -->
{% endblock %}
In this template, we first display the patient's name, email, and phone number. If the patient is a medical aid member, we display their medical aid information, including their medical aid provider, membership number, and category.
We can also include code to display the patient's medical history, consultations, and prescriptions, which we would need to define in the corresponding views and templates.
User
continue
Assistant
To display the patient's medical history, consultations, and prescriptions in the patient_detail.html template, we can include additional code that uses Django's template language to iterate over the relevant objects and display their details. Here's an example:
php
{% extends 'base.html' %}
{% block content %}
<h1>{{ patient.first_name }} {{ patient.last_name }}</h1>
<p>Email: {{ patient.email }}</p>
<p>Phone number: {{ patient.phone_number }}</p>
{% if patient.is_medical_aid_member %}
<h2>Medical Aid Information</h2>
<p>Provider: {{ medical_aid_details.Provider }}</p>
<p>Membership number: {{ medical_aid_details.Membership number }}</p>
<p>Category: {{ medical_aid_details.Category }}</p>
<!-- additional details about the patient's medical aid coverage -->
{% endif %}
{% if patient.medical_history %}
<h2>Medical History</h2>
<ul>
{% for history_item in patient.medical_history.all %}
<li>{{ history_item.date }} - {{ history_item.details }}</li>
{% endfor %}
</ul>
{% endif %}
{% if patient.consultations %}
<h2>Consultations</h2>
<ul>
{% for consultation in patient.consultations.all %}
<li>{{ consultation.date }} with {{ consultation.doctor }} - {{ consultation.details }}</li>
{% endfor %}
</ul>
{% endif %}
{% if patient.prescriptions %}
<h2>Prescriptions</h2>
<ul>
{% for prescription in patient.prescriptions.all %}
<li>{{ prescription.date }} prescribed by {{ prescription.doctor }} - {{ prescription.medication }}</li>
{% endfor %}
</ul>
{% endif %}
{% endblock %}
In this template, we first check if the patient has medical aid information and display it if they do. We then check if the patient has any medical history, consultations, or prescriptions and display them if they exist.
To display the medical history, we iterate over the medical_history objects related to the patient using the all method on the related manager. We display the date and details for each history item in an unordered list.
Similarly, to display the consultations, we iterate over the consultations objects related to the patient and display the date, doctor, and details in a list.
Finally, to display the prescriptions, we iterate over the prescriptions objects related to the patient and display the date, doctor, and medication in a list.
User
continue
Assistant
Now let's add functionality to handle patients who are medical aid members versus those who are private patients.
To do this, we need to modify our create_consultation view to calculate the consultation fee based on the patient's medical aid status. We can add a new field to the Consultation model to store the fee, and then use this field to display the fee in the consultation detail page. Here's an example implementation:
python
# models.py
class Consultation(models.Model):
date = models.DateTimeField()
doctor = models.ForeignKey(Doctor, on_delete=models.CASCADE)
patient = models.ForeignKey(Patient, on_delete=models.CASCADE, related_name='consultations')
details = models.TextField()
fee = models.DecimalField(max_digits=6, decimal_places=2, null=True, blank=True)
def calculate_fee(self):
if self.patient.is_medical_aid_member:
# calculate fee for medical aid member
medical_aid_scheme = self.patient.medical_aid_scheme
# use medical aid scheme information to calculate fee
# ...
self.fee = calculated_fee
else:
# calculate fee for private patient
self.fee = 100.00 # default fee for private patients
# views.py
def create_consultation(request, appointment_id):
appointment = get_object_or_404(Appointment, id=appointment_id)
patient = appointment.patient
doctor = appointment.doctor
if request.method == 'POST':
form = ConsultationForm(request.POST)
if form.is_valid():
consultation = form.save(commit=False)
consultation.date = timezone.now()
consultation.doctor = doctor
consultation.patient = patient
consultation.calculate_fee() # calculate fee based on patient's medical aid status
consultation.save()
return redirect('consultation_detail', consultation.id)
else:
form = ConsultationForm()
context = {
'appointment': appointment,
'patient': patient,
'doctor': doctor,
'form': form,
}
return render(request, 'create_consultation.html', context)
# consultation_detail.html
{% extends 'base.html' %}
{% block content %}
<h1>Consultation Details</h1>
<p>Date: {{ consultation.date }}</p>
<p>Doctor: {{ consultation.doctor }}</p>
<p>Patient: {{ consultation.patient }}</p>
<p>Details: {{ consultation.details }}</p>
{% if consultation.fee %}
<p>Fee: {{ consultation.fee }}</p>
{% endif %}
{% endblock %}
User
continue
Assistant
Finally, let's add support for storing medical aid membership information for patients.
We can add two new fields to the Patient model to store the medical aid provider and membership number. We can then use this information in the calculate_fee method of the Consultation model to look up the patient's medical aid scheme and calculate the consultation fee accordingly. Here's an example implementation:
css
User
continue
Assistant
to the Patient model: medical_aid_provider and medical_aid_membership_number. These fields will allow us to store information about the patient's medical aid provider and membership number.
We've also added a new MedicalAidScheme model to the project, which will allow us to look up the patient's medical aid scheme based on their provider and membership number.
In the Consultation model, we've updated the calculate_fee method to take into account the patient's medical aid membership status. If the patient is a medical aid member, we look up their medical aid scheme based on their provider and membership number and use that information to calculate the consultation fee. If the patient is not a medical aid member, we set the fee to a default value of 100.00.
In the PatientForm, we've added fields for is_medical_aid_member, medical_aid_provider, and medical_aid_membership_number to allow users to enter this information when creating a new patient.
In the patient_detail.html template, we've updated the patient details section to include the patient's medical aid provider and membership number if they are a medical aid member. We've also updated the consultation details section to include the fee if it has been calculated.
Overall, this implementation should provide a basic framework for an application that allows doctors to manage patient appointments and consultations, including information about the patient's medical history and medical aid status.
Note: This code assumes that the app is named "doctor_app". Also, you would need to create the templates folder in your app directory and add the base.html file in it. The base.html file is the base template that all other templates extend. You can customize it as per your needs.
This view retrieves the patient with the given ID and all consultations associated with the patient. It then constructs a list of dictionaries containing information about each consultation, including the appointment time, doctor's name, notes, prescription, and consultation fee. Finally, it passes the patient and medical history to the patient_detail.html template for display.
User
continue
Assistant
To determine whether a patient is a medical aid member or private, we can add a field to the Patient model indicating their membership status:
We can also modify the create_appointment view to set the consultation_fee field of the appointment based on whether the patient is a medical aid member:
python
from django.shortcuts import render, redirect
from .forms import AppointmentForm
from .models import Appointment, Patient
def create_appointment(request, patient_id):
patient = Patient.objects.get(id=patient_id)
if request.method == 'POST':
form = AppointmentForm(request.POST)
if form.is_valid():
appointment = form.save(commit=False)
appointment.patient = patient
if patient.medical_aid_member:
appointment.consultation_fee = 0.00
else:
appointment.consultation_fee = 100.00
appointment.save()
return redirect('patient_detail', patient_id=patient_id)
else:
form = AppointmentForm()
return render(request, 'create_appointment.html', {'form': form, 'patient': patient})
This view checks the medical_aid_member field of the patient and sets the consultation_fee field of the appointment accordingly. If the patient is a medical aid member, the fee is set to 0.00; otherwise, it is set to 100.00.
Finally, we can modify the Consultation model to include the prescription dispensed during the consultation:
python
class Consultation(models.Model):
appointment = models.OneToOneField(Appointment, on_delete=models.CASCADE)
doctor = models.ForeignKey(User, on_delete=models.CASCADE)
notes = models.TextField()
prescription = models.TextField()
created_at = models.DateTimeField(auto_now_add=True)
def calculate_fee(self):
# Calculate fee based on consultation details
return self.appointment.consultation_fee
def __str__(self):
return f"Consultation for {self.appointment.patient} on {self.appointment.appointment_time}"
User
continue
Assistant
With these modifications, we can now display the patient's medical history, consultations, and prescription dispensed on the patient detail page. Here's an example patient_detail.html template that displays this information:
html
<h1>{{ patient.full_name }}</h1>
{% if patient.medical_aid_member %}
<p>Medical Aid Provider: {{ patient.medical_aid_provider }}</p>
<p>Medical Aid Membership Number: {{ patient.medical_aid_number }}</p>
<p>Medical Aid Scheme: {{ patient.medical_aid_scheme }}</p>
{% endif %}
<h2>Medical History</h2>
{% if patient.medical_history %}
<p>{{ patient.medical_history }}</p>
{% else %}
<p>No medical history on record.</p>
{% endif %}
<h2>Consultations</h2>
{% if patient.appointments.all %}
<ul>
{% for appointment in patient.appointments.all %}
{% if appointment.consultation %}
<li>
<p><strong>Date:</strong> {{ appointment.appointment_time }}</p>
<p><strong>Doctor:</strong> {{ appointment.consultation.doctor }}</p>
<p><strong>Notes:</strong> {{ appointment.consultation.notes }}</p>
<p><strong>Prescription:</strong> {{ appointment.consultation.prescription }}</p>
</li>
{% endif %}
{% endfor %}
</ul>
{% else %}
<p>No consultations on record.</p>
{% endif %}
This template displays the patient's full name, medical aid information (if applicable), medical history, and a list of consultations. For each consultation, it displays the appointment date and time, the doctor who conducted the consultation, the notes taken during the consultation, and the prescription dispensed.
Note that this template assumes that the Appointment model has a consultation property that returns the associated Consultation object, as we defined in our earlier modifications. We can modify the Appointment model to include this property as follows:
This adds a consultation method to the model that returns the associated Consultation object, or None if no such object exists. We also modify the __str__ method to include the patient name and appointment time.
User
continue
Assistant
Now that we have modified our models and templates to display medical history, consultations, and prescription information, we can implement the logic for determining whether a patient is a medical aid member or a private patient, and for calculating consultation fees accordingly.
First, we need to add the MedicalAidProvider and MedicalAidScheme models to our models.py file:
python
class MedicalAidProvider(models.Model):
name = models.CharField(max_length=100)
contact_number = models.CharField(max_length=20)
address = models.TextField()
def __str__(self):
return self.name
class MedicalAidScheme(models.Model):
name = models.CharField(max_length=100)
provider = models.ForeignKey(MedicalAidProvider, on_delete=models.CASCADE)
membership_number = models.CharField(max_length=20)
def __str__(self):
return self.name
The MedicalAidProvider model represents a medical aid provider, while the MedicalAidScheme model represents a specific medical aid scheme offered by that provider. Each MedicalAidScheme object has a foreign key to a MedicalAidProvider object, representing the provider that offers the scheme.
Next, we need to modify the Patient model to include the relevant medical aid information, and to implement the calculate_fee method to calculate consultation fees based on whether the patient is a medical aid member or a private patient:
python
class Patient(models.Model):
# ...existing fields...
medical_aid_member = models.BooleanField(default=False)
medical_aid_provider = models.ForeignKey(MedicalAidProvider, on_delete=models.CASCADE, blank=True, null=True)
medical_aid_number = models.CharField(max_length=20, blank=True)
medical_aid_scheme = models.ForeignKey(MedicalAidScheme, on_delete=models.CASCADE, blank=True, null=True)
def calculate_fee(self):
# Calculate fee based on consultation details and patient type
if self.medical_aid_member:
return 0.00
else:
total_fee = 0.00
for appointment in self.appointments.all():
total_fee += appointment.consultation_fee
return total_fee
def __str__(self):
return self.full_name
User
continue
Assistant
At this point, we have defined our models, views, and templates for a basic doctor appointment application in Django that includes patient medical history, consultations, and medical prescription dispensed. We have also implemented functionality to differentiate between medical aid members and private patients, and to calculate fees based on this information.
However, there are still some additional features that could be implemented to enhance the functionality of this application. For example, we could add the ability for patients to book appointments online, send reminders to patients about upcoming appointments, and provide a way for patients to leave feedback or ratings for their consultations.
Additionally, we could integrate with external services such as pharmacies to automatically order and dispense medications for patients, or integrate with insurance companies to automatically submit claims and process payments.
Overall, there are many possibilities for enhancing this application and making it more useful for both doctors and patients.
With these models, we can create forms for the user to input data and views to process the form data and save it to the database.
For example, to add a new MedicalHistory object for a patient, we can create a form with fields for the condition, diagnosis date, and treatment notes, and a view to process the form data and save it to the database:
python
# forms.py
from django import forms
from .models import MedicalHistory
class MedicalHistoryForm(forms.ModelForm):
class Meta:
model = MedicalHistory
fields = ['condition', 'diagnosis_date', 'treatment_notes']
# views.py
from django.shortcuts import render, redirect
from .models import Patient, MedicalHistory
from .forms import MedicalHistoryForm
def create_medical_history(request, patient_id):
patient = Patient.objects.get(id=patient_id)
if request.method == 'POST':
form = MedicalHistoryForm(request.POST)
if form.is_valid():
medical_history = form.save(commit=False)
medical_history.patient = patient
medical_history.save()
return redirect('patient_detail', patient_id=patient.id)
else:
form = MedicalHistoryForm()
return render(request, 'create_medical_history.html', {'form': form, 'patient': patient})
In this view, we first retrieve the Patient object with the specified patient_id. Then, if the request method is POST, we create a new MedicalHistory object with the patient and user input and save it to the database. Finally, we redirect to the patient detail page. If the request method is not POST, we render the create_medical_history.html template with the form and patient objects as context.
We can create similar forms and views for Consultation and MedicalPrescription objects.
We can then use these fields to retrieve information about the patient's medical aid coverage, if applicable. For example, we could create a view that displays the patient's medical aid information and any relevant medical aid scheme details:
python
# views.py
def patient_detail(request, patient_id):
patient = Patient.objects.get(id=patient_id)
if patient.is_medical_aid_member:
medical_aid_details = {
'Provider': patient.medical_aid_provider,
'Membership number': patient.medical_aid_membership_number,
'Category': patient.medical_aid_category,
# additional details about the patient's medical aid coverage
}
else:
medical_aid_details = None
return render(request, 'patient_detail.html', {'patient': patient, 'medical_aid_details': medical_aid_details})
In this view, we first retrieve the Patient object with the specified patient_id. If the patient is a medical aid member, we create a dictionary of the patient's medical aid details, including their medical aid provider, membership number, and category. Otherwise, we set medical_aid_details to None.
We can then pass this information to the patient_detail.html template, where we can display it
In this implementation, we've added a fee field to the Consultation model, which will store the fee for the consultation. We've also added a calculate_fee method to the model that calculates the fee based on the patient's medical aid status and stores it in the fee field.
In the create_consultation view, we call the calculate_fee method on the Consultation object before saving it to calculate the fee and store it in the database. We can then display the fee in the consultation detail page if it exists.
Note that in this implementation, we've used a simple default fee of $100.00 for private patients. In a real application, you would likely want to provide more fine-grained control over consultation fees for private patients, such as allowing doctors to set their own fees or setting fees based on the complexity of the consultation. Additionally, you would need to handle the case where a medical aid scheme does not cover a particular consultation, and provide appropriate messaging to the patient in such cases.
# models.py
class Patient(models.Model):
first_name = models.CharField(max_length=30)
last_name = models.CharField(max_length=30)
date_of_birth = models.DateField()
is_medical_aid_member = models.BooleanField(default=False)
medical_aid_provider = models.CharField(max_length=50, null=True, blank=True)
medical_aid_membership_number = models.CharField(max_length=50, null=True, blank=True)
class Consultation(models.Model):
date = models.DateTimeField()
doctor = models.ForeignKey(Doctor, on_delete=models.CASCADE)
patient = models.ForeignKey(Patient, on_delete=models.CASCADE, related_name='consultations')
details = models.TextField()
fee = models.DecimalField(max_digits=6, decimal_places=2, null=True, blank=True)
def calculate_fee(self):
if self.patient.is_medical_aid_member:
# calculate fee for medical aid member
medical_aid_provider = self.patient.medical_aid_provider
medical_aid_membership_number = self.patient.medical_aid_membership_number
try:
medical_aid_scheme = MedicalAidScheme.objects.get(provider=medical_aid_provider, membership_number=medical_aid_membership_number)
# use medical aid scheme information to calculate fee
# ...
self.fee = calculated_fee
except MedicalAidScheme.DoesNotExist:
self.fee = None # unable to find medical aid scheme for patient
else:
# calculate fee for private patient
self.fee = 100.00 # default fee for private patients
# forms.py
class PatientForm(forms.ModelForm):
class Meta:
model = Patient
fields = ['first_name', 'last_name', 'date_of_birth', 'is_medical_aid_member', 'medical_aid_provider', 'medical_aid_membership_number']
# views.py
def create_patient(request):
if request.method == 'POST':
form = PatientForm(request.POST)
if form.is_valid():
patient = form.save()
return redirect('patient_detail', patient.id)
else:
form = PatientForm()
context = {
'form': form,
}
return render(request, 'create_patient.html', context)
# patient_detail.html
{% extends 'base.html' %}
{% block content %}
<h1>Patient Details</h1>
<p>First Name: {{ patient.first_name }}</p>
<p>Last Name: {{ patient.last_name }}</p>
<p>Date of Birth: {{ patient.date_of_birth }}</p>
{% if patient.is_medical_aid_member %}
<p>Medical Aid Provider: {{ patient.medical_aid_provider }}</p>
<p>Medical Aid Membership Number: {{ patient.medical_aid_membership_number }}</p>
{% endif %}
<h2>Consultations</h2>
{% for consultation in patient.consultations.all %}
<p>Date: {{ consultation.date }}</p>
<p>Doctor: {{ consultation.doctor }}</p>
<p>Details: {{ consultation.details }}</p>
{% if consultation.fee %}
<p>Fee: {{ consultation.fee }}</p>
{% endif %}
{% endfor %}
{% endblock %}
In this implementation, we've added two new fields
from django import forms
from .models import Patient
class PatientForm(forms.ModelForm):
class Meta:
model = Patient
fields = '__all__'
from django import forms
from .models import Patient, Consultation
class PatientForm(forms.ModelForm):
class Meta:
model = Patient
fields = ('first_name', 'last_name', 'date_of_birth', 'is_medical_aid_member', 'medical_aid_provider', 'medical_aid_membership_number')
class ConsultationForm(forms.ModelForm):
class Meta:
model = Consultation
fields = ('appointment_time', 'symptoms', 'diagnosis', 'prescription')
This model includes the MedicalAidProvider, MedicalAidScheme, Patient, Appointment, and Consultation models. The MedicalAidProvider and MedicalAidScheme models allow for the definition of medical aid providers and schemes, respectively. The Patient model includes a one-to-one relationship with the built-in User model to represent patients, as well as a medical_aid_number field and a foreign key to MedicalAidScheme to allow for the definition of a patient's medical aid status. The Appointment model includes a foreign key to Patient and a date/time field to represent appointments, as well as a consultation_fee field to allow for the calculation of consultation fees. Finally, the Consultation model includes a one-to-one relationship with Appointment, a foreign key to User to represent doctors, and fields for notes and prescriptions.
This modified view sets the payment_type variable in the context based on the patient's medical_aid_member field. We can then display this information in the patient_detail.html template as follows:
html
{% if payment_type == 'Medical Aid' %}
<p>Payment type: Medical Aid</p>
{% else %}
<p>Payment type: Cash</p>
{% endif %}
Finally, we can update the patient creation form to include fields for the patient's medical aid provider, medical aid scheme, and medical aid number:
python
from django import forms
from .models import Patient
class PatientForm(forms.ModelForm):
class Meta:
model = Patient
fields = ['first_name', 'last_name', 'email', 'medical_aid_member', 'medical_aid_provider', 'medical_aid_scheme', 'medical_aid_number']
This form includes fields for medical_aid_member, medical_aid_provider, medical_aid_scheme, and medical_aid_number, which are all added to the Patient model. When the form is submitted, the data is saved to the database and can be retrieved later using the patient_detail view.
We add a prescription field to the model and modify the __str__ method to include the patient name and appointment time. We can then modify the create_consultation view to include a field for the prescription:
python
from django.shortcuts import render, redirect
from .forms import ConsultationForm
from .models import Appointment, Consultation
def create_consultation(request, appointment_id):
appointment = Appointment.objects.get(id=appointment_id)
if request.method == 'POST':
form = ConsultationForm(request.POST)
if form.is_valid():
consultation = form.save(commit=False)
consultation.appointment = appointment
consultation.doctor = request.user
consultation.save()
return redirect('patient_detail', patient_id=appointment.patient.id)
else:
form = ConsultationForm()
return render(request, 'create_consultation.html', {'form': form, 'appointment': appointment})
We update the ConsultationForm to include a field for the prescription, and modify the view to set the appointment and doctor fields of the consultation before saving it to the database.
In this modified Patient model, we have added four new fields: medical_aid_member (a boolean indicating whether the patient is a medical aid member), medical_aid_provider (a foreign key to the MedicalAidProvider model), medical_aid_number (the patient's medical aid membership number), and medical_aid_scheme (a foreign key to the MedicalAidScheme model).
We have also modified the calculate_fee method to return a consultation fee of 0.00 if the patient is a medical aid member. Otherwise, it calculates the total consultation fee by summing the consultation fees for all appointments associated with the patient.
Finally, we need to modify the Appointment model to set the consultation fee based on whether the patient is a medical aid member or a private patient:
python
class Appointment(models.Model):
# ...existing fields...
consultation_fee = models.DecimalField(max_digits=5, decimal_places=2, default=0.00)
def save(self, *args, **kwargs):
if self.patient.medical_aid_member:
self.consultation_fee = 0.00
else:
self.consultation_fee = self.consultation.calculate_fee()
super().save(*args, **kwargs)
def __str__(self):
return f"Appointment for {self.patient} on {self.appointment_time}"